-3

i have an app android. all code is ok with no error, but i don't know my app sometimes have a force close. Can you help me to fix this?

here's my 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)

Here is my code :

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);

    }
}

2 Answers2

1

It seems that the decision in this answer.

if(!((Activity) context).isFinishing())
{
    //show dialog
}

All the other answers out there seem to be doing weird things like iterating through the list of running activities, but this is much simpler and seems to do the trick.

Community
  • 1
  • 1
user2413972
  • 1,355
  • 2
  • 9
  • 25
0

please try below code. modify it according to your need

I faced exactly the same issue. Calling '(!isFinishing())' prevented the crash, but it could not display the 'alert' message.

Then I tried making calling function 'static', where alert is displayed. After that, no crash happened and message is also getting displayed.

Example

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();      
    }