在我的按钮的 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.