您好我在Message对话框中遇到问题,强制关闭我的代码就在这里。
in on create:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.email_result);
email_result = (Button) findViewById(R.id.email_result_btn);
email_result.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (diffdays > 365) {
h.sendEmptyMessage(0);
}
}
}
}
我的处理程序:
private Handler h = new Handler() {
public void handleMessage(Message msg) {
showMessageDialog("Sorry, you cannot email entries which are earlier than one year ago.");
}
};
ShowMessageDialog方法:
public void showMessageDialog(String nMessage) {
alertDialog = new Dialog(Email_Result.this);
AlertDialog.Builder customBuilder = new AlertDialog.Builder(
Email_Result.this);
customBuilder.setMessage(nMessage);
customBuilder.setPositiveButton(getString(R.string.ok),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog = customBuilder.create();
alertDialog.setCancelable(true);
alertDialog.show();
}
错误记录
01-11 12:08:24.470: ERROR/AndroidRuntime(325): FATAL EXCEPTION: main
01-11 12:08:24.470: ERROR/AndroidRuntime(325): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44f1dfd8 is not valid; is your activity running?
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at android.view.ViewRoot.setView(ViewRoot.java:505)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at android.view.Window$LocalWindowManager.addView(Window.java:424)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at android.app.Dialog.show(Dialog.java:241)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at com.stress1.Email_Result.showMessageDialog(Email_Result.java:207)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at com.stress1.Email_Result$2.onClick(Email_Result.java:81)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at android.view.View.performClick(View.java:2408)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at android.view.View$PerformClick.run(View.java:8816)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at android.os.Handler.handleCallback(Handler.java:587)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at android.os.Handler.dispatchMessage(Handler.java:92)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at android.os.Looper.loop(Looper.java:123)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at java.lang.reflect.Method.invoke(Method.java:521)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-11 12:08:24.470: ERROR/AndroidRuntime(325): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:26)
当您在Dialog上调用show()方法时,似乎发生了异常。尝试使用以下代码来解决您的问题:
try {
alertDialog.show();
} catch(Exception e){
// WindowManager$BadTokenException will be caught and the app would not display
// the 'Force Close' message
}
当活动试图在已经终止后显示AlertDialog时会出现这样的问题。因此,您可能希望仔细查看代码的工作原理。
此外,您的showMessageDialog方法可以简化如下:
public void showMessageDialog(String nMessage) {
AlertDialog.Builder customBuilder = new AlertDialog.Builder(Email_Result.this);
customBuilder.setMessage(nMessage);
customBuilder.setPositiveButton(getString(R.string.ok),new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
customBuilder.setCancelable(true);
customBuilder.show();
}
答案 1 :(得分:5)
只需在代码中添加if(!isFinishing),如下所示:
private Handler h = new Handler() {
public void handleMessage(Message msg) {
if(!isFinishing)
showMessageDialog("Sorry, you cannot email entries which are earlier than one year ago.");
}
};
答案 2 :(得分:0)
使用以下内容,您将不会遇到Fatal Exception: android.view.WindowManager$BadTokenException
异常
if(!Email_Result.this.isFinishing())
{
customBuilder.show();
}
如果您有context
,则可以按以下方式使用:
if(!((Activity) context).isFinishing())
{
customBuilder.show();
}