Dialog problem: requestFeature() must be called before adding content

前端 未结 9 559
名媛妹妹
名媛妹妹 2020-12-31 02:39

I\'m creating a custom dialog containing an EditText so that I can get text data from the user:

final EditText newKey = (EditText) findViewById(R.id.dialog_r         


        
9条回答
  •  醉话见心
    2020-12-31 03:19

    This also happens if you try to access any view item with findViewById() before the onCreate() That is in my case i did:

     private class ShareViaDialog extends Dialog  {
     private ImageView mainSmallIcon = findViewById(R.id.mainSmallIcon);    //this is wrong
      @Override
            protected void onCreate(Bundle savedInstanceState) {
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                super.onCreate(savedInstanceState);
    }}
    

    so the correct way to avoid this error is:

     private class ShareViaDialog extends Dialog  {
         private ImageView mainSmallIcon;
          @Override
                protected void onCreate(Bundle savedInstanceState) {
                    requestWindowFeature(Window.FEATURE_NO_TITLE);
                    super.onCreate(savedInstanceState);
    mainSmallIcon = findViewById(R.id.mainSmallIcon); //should be here
        }}
    

提交回复
热议问题