Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertBuilder showing an exception. DialogBox is not opening [duplicate]

Tags:

android

In the following code I want to open the dialog box when the mIndex is 0.

The code is going in the 'if' part. But is not opening the dialog Box instead showing the following exception.

Code:

mIndex = (mIndex+1)%mQuestionBank.length;
    if(mIndex == 0 )
    {
        Log.d("Quizzler", "Inside IF");
        AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());
        alert.setTitle("Game Over");
        alert.setCancelable(false);
        alert.setMessage("Your score is "+mScore);
        alert.setPositiveButton("Close Application", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        alert.show();
    }

Exception:

FATAL EXCEPTION: main
                                                                  Process: com.londonappbrewery.quizzler, PID: 11560                                                                               android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
                                                                               at android.view.ViewRootImpl.setView(ViewRootImpl.java:714)
                                                                               at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
                                                                               at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
                                                                               at android.app.Dialog.show(Dialog.java:316)
                                                                               at android.app.AlertDialog$Builder.show(AlertDialog.java:1112)
                                                                               at com.londonappbrewery.quizzler.MainActivity.updateQuestion(MainActivity.java:100)
                                                                               at com.londonappbrewery.quizzler.MainActivity.access$100(MainActivity.java:16)
                                                                               at com.londonappbrewery.quizzler.MainActivity$1.onClick(MainActivity.java:69)
                                                                               at android.view.View.performClick(View.java:5609)
                                                                               at android.view.View$PerformClick.run(View.java:22263)
                                                                               at android.os.Handler.handleCallback(Handler.java:751)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
like image 438
Ajinkya jadhav Avatar asked Dec 04 '25 19:12

Ajinkya jadhav


1 Answers

Instead of

new AlertDialog.Builder(getApplicationContext());

Try using

new AlertDialog.Builder(MainActivity.this);
like image 84
Aswin P Ashok Avatar answered Dec 07 '25 09:12

Aswin P Ashok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!