AlertDialog cannot display custom layout

I am using the following piece of code to show a custom dialog:

btnCancel.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        // 1. Instantiate an AlertDialog.Builder with its constructor
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        // 2. Chain together various setter methods to set the dialog
        // characteristics
        builder.setTitle("Question");

        AlertDialog dialog= builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
        {

            public void onClick(DialogInterface dialog, int whichButton)
            {
                dialog.dismiss();
                CallMethod();
            }

        }).setNegativeButton("No",  new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
            }
        }).create();
        dialog.setContentView(R.layout.question);
        dialog.show();
    }
});

      

When I click the button, I get this exception:

android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:229)
at com.android.internal.app.AlertController.installContent(AlertController.java:234)
at android.app.AlertDialog.onCreate(AlertDialog.java:337)
at android.app.Dialog.dispatchOnCreate(Dialog.java:355)
at android.app.Dialog.show(Dialog.java:260)
at com.example.MyApp.SimpleActivity$2.onClick(SimpleActivity.java:108)
at android.view.View.performClick(View.java:4207)
at android.view.View$PerformClick.run(View.java:17372)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

      

What request is needed?

+3


source to share


5 answers


The code snippet is good, the error is this: the context field was set to a different action. I changed it to the following and now it works:

AlertDialog.Builder builder = new AlertDialog.Builder(SimpleActivity.this);

      



Your advice is appreciated.

0


source


You need to call setView

instead setContentView

:

dialog.setView(R.layout.question);

      

And set the view before creating the dialog:



dialog.setView(R.layout.question).create();

      

[Edit]

 AlertDialog dialog= builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
  {

    public void onClick(DialogInterface dialog, int whichButton)
    {
      dialog.dismiss();
      CallMethod();
    }

  }).setNegativeButton("No",  new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface dialog, int which)
        {
          dialog.dismiss();
        }
      }).setView(R.layout.question).create();

      

+3


source


Before creating the dialog box, you need to set a custom view.

Try to use

 AlertDialog.Builder builder = new AlertDialog.Builder(context)
    .setTitle("Question")
    .setView()
    .setPositiveButton()
    .setNegativeButton()
    .create().show();
  }
});

      

+1


source


android.util.AndroidRuntimeException: requestFeature () must be called before adding content

You must place the requestFeature before setting the content view. Example:

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenu);
}

      

NOT

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenu);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
}

      

0


source


It's probably best to write your own dialog class:

 private class CustomDialogClass extends Dialog implements View.OnClickListener { 
public CustomDialogClass(Activity a, String picturename) {
        super(a);
        this.c = a;
        this.picturename = picturename;

      

...

set in layout your layout:

 super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialoglayout);

      

and open it like this:

CustomDialogClass cdd = new CustomDialogClass();
                            cdd.getWindow());
                            cdd.show();

      

0


source







All Articles