我使用以下代码段来显示自定义对话框:
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)
需要什么样的要求?
答案 0 :(得分: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();
答案 1 :(得分:1)
您需要在创建对话框之前设置自定义视图。
尝试使用
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setTitle("Question")
.setView()
.setPositiveButton()
.setNegativeButton()
.create().show();
}
});
答案 2 :(得分:0)
android.util.AndroidRuntimeException:在添加内容之前必须调用requestFeature()
在设置内容视图之前,必须放置requestFeature。例如:
@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);
}
答案 3 :(得分: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();
答案 4 :(得分:0)
代码段很好,错误是这样的:字段上下文 设置到另一个Activity。我将其更改为以下内容,现在可以使用了:
AlertDialog.Builder builder = new AlertDialog.Builder(SimpleActivity.this);
您的提示很受欢迎。