我有一个应用程序android。所有代码都没有错误,但我不知道我的应用程序有时会关闭一个力量。你能帮我解决这个问题吗?
这是我的logcat:
09-17 02:35:36.352: E/AndroidRuntime(2031): FATAL EXCEPTION: main
09-17 02:35:36.352: E/AndroidRuntime(2031): Process: com.sendquiz.guessanimalsquiz, PID: 2031
09-17 02:35:36.352: E/AndroidRuntime(2031): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@b1f03218 is not valid; is your activity running?
09-17 02:35:36.352: E/AndroidRuntime(2031): at android.view.ViewRootImpl.setView(ViewRootImpl.java:532)
09-17 02:35:36.352: E/AndroidRuntime(2031): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
09-17 02:35:36.352: E/AndroidRuntime(2031): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
09-17 02:35:36.352: E/AndroidRuntime(2031): at android.app.Dialog.show(Dialog.java:286)
09-17 02:35:36.352: E/AndroidRuntime(2031): at com.sendquiz.javafile.TimeCompleteDialog.showDialog(TimeCompleteDialog.java:47)
09-17 02:35:36.352: E/AndroidRuntime(2031): at com.sendquiz.guessanimalsquiz.QuizActivity.onTimeFinish(QuizActivity.java:240)
09-17 02:35:36.352: E/AndroidRuntime(2031): at com.sendquiz.guessanimalsquiz.QuizActivity$1.run(QuizActivity.java:49)
09-17 02:35:36.352: E/AndroidRuntime(2031): at android.os.Handler.handleCallback(Handler.java:733)
09-17 02:35:36.352: E/AndroidRuntime(2031): at android.os.Handler.dispatchMessage(Handler.java:95)
09-17 02:35:36.352: E/AndroidRuntime(2031): at android.os.Looper.loop(Looper.java:136)
09-17 02:35:36.352: E/AndroidRuntime(2031): at android.app.ActivityThread.main(ActivityThread.java:5001)
09-17 02:35:36.352: E/AndroidRuntime(2031): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 02:35:36.352: E/AndroidRuntime(2031): at java.lang.reflect.Method.invoke(Method.java:515)
09-17 02:35:36.352: E/AndroidRuntime(2031): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-17 02:35:36.352: E/AndroidRuntime(2031): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-17 02:35:36.352: E/AndroidRuntime(2031): at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
public void gameCompleted(){
GameCompleteDialog dialog=new GameCompleteDialog(QuizActivity.this,SCORE_COUNTER);
dialog.buildDialog();
dialog.showDialog();
handler.removeCallbacks(timerThread);
}
public void onTimeFinish() {
stopTimer();
TimeCompleteDialog dialog=new TimeCompleteDialog(this);
dialog.showDialog();
AnswerHandler.vibrate(this);
}
}
答案 0 :(得分:1)
似乎是answer中的决定。
if(!((Activity) context).isFinishing())
{
//show dialog
}
所有其他答案似乎都在做一些奇怪的事情,比如遍历正在运行的活动列表,但这样做更简单,似乎可以解决问题。
答案 1 :(得分:0)
请尝试以下代码。根据您的需要进行修改
我遇到了完全相同的问题。拨打电话'(!isFinishing())'防止崩溃,但它无法显示“警报”。消息。
然后我尝试调用函数' static',显示警报。之后,没有发生崩溃,也显示了消息。
实施例
public static void connect_failure() {
Log.i(FW_UPD_APP, "Connect failed");
new AlertDialog.Builder(MyActivity)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do nothing
}
})
.setIcon(R.drawable.the_image).show();
}