简体   繁体   中英

App crashing whilst displaying a dialog

Im getting an error when i'm trying to call this method:

        okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, final IOException e) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showErrorAlert(e.toString());
                }
            });
        }

//The Method:

public void showErrorAlert(String error) {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(ProjectListActivity.this);

    dialogBuilder.setMessage("Hmm, there seems to be an error downloading the project list. " + error);
    dialogBuilder.setCancelable(true);

    dialogBuilder.setPositiveButton(
            "Okay",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = dialogBuilder.create();
    alert.show();
}

So when the call fails, it will then instantly crash and this is the output in the console:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.chimesoftware.chime.chimetimemanager, PID: 5770
                  android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@a9b7fff is not valid; is your activity running?
                      at android.view.ViewRootImpl.setView(ViewRootImpl.java:925)
                      at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
                      at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
                      at android.app.Dialog.show(Dialog.java:330)
                      at com.chimesoftware.chime.chimetimemanager.ProjectListActivity.showErrorAlert(ProjectListActivity.java:145)
                      at com.chimesoftware.chime.chimetimemanager.ProjectListActivity$1$1.run(ProjectListActivity.java:77)
                      at android.os.Handler.handleCallback(Handler.java:790)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6753)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:482)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Is this possible due to calling the error wrong? or possibly because of the type of error I am trying to display.

Thank you.

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

As the exception message says, the current activity has finished but you are trying to display a dialog with a context of the finished activity. Since there is no window for the dialog to display the android runtime throws this exception.

Use Activity isFinishing() method:

Check to see whether this activity is in the process of finishing, either because you called finish() on it or someone else has requested that it finished.

if(!isFinishing())
showDialog();

It is working for me, if you want to use that :

AlertDialog.Builder alertDialog = new AlertDialog.Builder(HelpAndSupport.this);
    alertDialog.setTitle("Confirm Sign out...");
    alertDialog.setMessage("Are you sure you want signout from Talentslist?");
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            //you posive click code here
        }
    });

    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    alertDialog.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM