AlertDialog无法显示自定义布局

3

我使用以下代码片段展示自定义对话框:

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();
    }
});

当我点击按钮时,出现了以下异常:
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)

需要什么样的请求?

1
在这里查看自定义警报对话框 - androhub.com/android-alert-dialog/ - Surender Kumar
5个回答

3
你需要调用setView而不是setContentView:
dialog.setView(R.layout.question);

在创建对话框之前设置视图:

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

[编辑]

 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();

AlertDialog 没有 create 函数。只有 AlertDialog.Builder 有,但它不起作用。 - Nestor
请看编辑后的答案。我的意思是创建Builder方法。你试过像编辑后的答案那样吗? - Green goblin

1
在创建对话框之前,您需要设置自定义视图。
尝试使用
 AlertDialog.Builder builder = new AlertDialog.Builder(context)
    .setTitle("Question")
    .setView()
    .setPositiveButton()
    .setNegativeButton()
    .create().show();
  }
});

如果我这样做,当我点击按钮时什么都不会发生 - 没有异常,也没有对话框。 - Nestor
改变顺序不会改变任何东西。视图未显示。 - Nestor

0
android.util.AndroidRuntimeException: 必须在添加内容之前调用requestFeature()。
你必须在设置内容视图之前放置requestFeature。例如:
@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenu);
}

不是:

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

我不使用_requestWindowFeature_,所以你的回答对我没有帮助。 - Nestor
是的,看起来当您在创建对话框之前没有设置自定义视图时,也会引发此错误。请参见此处的答案:https://dev59.com/42445IYBdhLWcg3w3N2z - Jonty800

0

最好编写自己的对话框类:

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

...

在onCreate方法中设置你的布局:
 super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialoglayout);

并像这样打开:

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

0

代码片段很好,错误在于:字段context被设置为另一个Activity。我将其更改为以下内容,现在它可以正常工作:

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

非常感谢您的提示。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接