在我的按钮的onClick方法中,我有此代码显示带有EditText字段的警告对话框。但是,每次单击按钮时,应用程序都会崩溃。我做错了什么?
AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
alert.setTitle("Enter password")
.setMessage("Enter your certificate's password");
final EditText input = new EditText(mContext);
alert.setView(input);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// code here
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.cancel();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
这是日志猫:
06-06 17:00:48.810: E/com.fx.safe.android.SafeApplication(23420): Thread uncaught exception:
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at android.app.Dialog.show(Dialog.java:281)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at com.android.lexperts.email.activity.MessageViewFragmentBase.onImportAttachment(MessageViewFragmentBase.java:819)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at com.android.lexperts.email.activity.MessageViewFragmentBase.onClick(MessageViewFragmentBase.java:1030)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at com.android.lexperts.email.activity.MessageViewFragment.onClick(MessageViewFragment.java:431)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at android.view.View.performClick(View.java:4202)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at android.view.View$PerformClick.run(View.java:17340)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at android.os.Handler.handleCallback(Handler.java:725)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at android.os.Looper.loop(Looper.java:137)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-06 17:00:48.810: E/com.fx.safe.android.safeApplication(23420): at dalvik.system.NativeStart.main(Native Method)
06-06 17:00:48.818: E/ACRA(23420): ACRA caught a BadTokenException exception for com.fx.safe.android. Building report.
答案 0 :(得分:1)
根据this问题的答案,当您传入错误的上下文时,会发生WindowManager.BadTokenException
。
所以将代码更改为:
AlertDialog.Builder alert = new AlertDialog.Builder(MyActivity.this);
[...]
final EditText input = new EditText(MyActivity.this);