0

我有一个自定义警报。我为它制作了一个自定义布局和一个扩展 Dialog 的类,其中我有几个定义警报行为的函数。我通过单击按钮从活动中调用该自定义警报。

一切正常,直到我想将handler.postDelayed添加到对话框中。

这是我的 Dialog 类中的一些代码:

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);

}

这是 tickOne 可运行的:

Runnable tickOne = new Runnable() {

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

    }
};

tickTwo 方法相同,只是设置了另一个文本。

当应用程序崩溃时,它在我调用对话框的活动中显示错误,我将错误追溯到这一行:

        dialog.show();

我发现如果我评论 handler.postDelayed 方法,我的对话框将按预期显示和消失。

所以,我的问题是 - 为什么自定义对话框不支持 postDelayed 方法,我该如何解决这个问题?

4

1 回答 1

1

您需要在 UI 线程上显示对话框。执行以下操作:

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
      }
    });
  }
}
于 2013-11-27T10:15:53.593 回答