Sam97305421562
Sam97305421562

Reputation: 3037

Showing dialog from run() method

I am trying to pop up dialog from run method it gives me exception that Looper.prepare not called, when I call the same method i dont get any exception but there is no pop up dialog shown on the console. As I have used handler in this way ,

handler = new Handler() {
    public void handleMessage(Message msg) {
        showDialog(DIALOG1_KEY);
        // process incoming messages here
    }
};

I am not getting any exception again but still no luck can any body tell me where I am doing things wrong.

Upvotes: 1

Views: 341

Answers (1)

Reto Meier
Reto Meier

Reputation: 96916

It's hard to tell from the code snippet you've provided, but I think you're using the Handler incorrectly.

What you need to do is initialize a new Handler object on them main thread, for example by defining it as a field variable.

private Handler handler = new Handler();

Then create a new Runnable that includes the instructions you want to execute on the GUI thread (but which will be called from your background thread's run method).

private Runnable runOnGUI = new Runnable() {
  private void run() {
    showDialog(DIALOG1_KEY);
  }
};

Then within your run method you need use the handler object to post your runOnGUI method on the GUI thread.

private Runnable runInBackground = new Runnable() {
  private void run() {
    handler.post(runOnGUI);
    // Do processing
  }
};

Upvotes: 3

Related Questions