1

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.

Joshua Best
  • 246
  • 1
  • 14

2 Answers2

0
if(!((Activity) context).isFinishing()){
    //show dialog here
}
  • 2
    While this code snippet may be the solution, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Narendra Jadhav Jul 26 '18 at 07:18
0

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();
amit
  • 659
  • 1
  • 8
  • 21