0

I have a custom alert. I made a custom layout for it and a class that extends Dialog where i have several functions defining alert's behavior. I call that custom alert from an activity, by clicking on a button.

Everything works fine until I want to ADD handler.postDelayed to the Dialog.

Here is a bit of code from my Dialog class:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.bonus_dialog);

    handler.postDelayed(tickOne, 900);
    handler.postDelayed(tickTwo, 1800);

}

Here is tickOne runnable:

Runnable tickOne = new Runnable() {

    @Override
    public void run() {
        countdown.setText("00:04");

    }
};

tickTwo method is the same, only setting another text.

When the app crashes it shows an error in the activity from where I call the Dialog, and I trace the error back to this line:

        dialog.show();

I figured out that if i comment handler.postDelayed methods, my dialog will be shown and disappear as intended.

So, my question is - why isn't postDelayed method supported in custom dialogs and how can I go around this?

SteBra
  • 4,188
  • 6
  • 37
  • 68

1 Answers1

1

You need to show the dialog on the UI thread. Do something like:

final SomeActivity activity = this.
Runnable tick1 = new Runnable() {
  public void run(){
    countdown.setText("00:04");
    activity.runOnUIThread(new Runnable() {
      public void run(){
        countdown.show(); // assuming the countdown is the dialog you want to show
      }
    });
  }
}
meredrica
  • 2,563
  • 1
  • 21
  • 24
  • looking at your answer, I figured out what was he solution. It wasn't needed to show dialog on UI thread, like you proposed BUT, I just had to make Handler ***final***, and now it works like a charm ! – SteBra Nov 27 '13 at 10:27