0

What does the following exception mean; how can I fix it? This is the code:

    if (response.isSuccessful()) {
                        String message = response.body().string();
                        JSONObject jsonObject = new JSONObject(message);

                        new AlertDialog.Builder(mcontext)
                                .setTitle("title:")
                                .setMessage(TmpPwd)
                                .setPositiveButton("close", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                    }
                                })
                                .show();
                    }

This is the exception:

    W/System.err: java.lang.RuntimeException: Can't create handler inside thread Thread[Thread-5,5,main] that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:205)
Richard
  • 23
  • 1
  • 4

2 Answers2

4

Inside your onResponse show your dialog like below inside a UI thread

activity.runOnUiThread(new Runnable() {
  public void run() {
  new AlertDialog.Builder(mcontext)
       .setTitle("title:")
       .setMessage(TmpPwd)
       .setPositiveButton("close", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
        }
    })
    .show();
  }
});
Abid Khan
  • 2,451
  • 4
  • 22
  • 45
1
Handler h = new Handler(Looper.getMainLooper());
 h.post(new Runnable() {
  public void run() {
  //here show dialog
}
});
Jinal Awaiya
  • 441
  • 3
  • 14