tags:

views:

124

answers:

1

hi, 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. Thnx in advance.

+3  A: 

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
  }
};
Reto Meier
You can also use the runOnUiThread(Runnable) method, available since Android 1.1 - it does exactly the same thing but with about 1 less line of code.
Isaac Waller