android dialog 自定义布局,android-AlertDialog无法显示自定义布局

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

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)

需要什么样的要求?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值