Android 处理程序问题

发布于 2024-10-11 11:07:20 字数 1241 浏览 5 评论 0原文

我确信我在这里做了一些愚蠢的事情,但是以下代码:

...

public void onClick(View v) {
  extractThread et = new extractThread();
  et.start();
}

...

private class extractThread extends Thread{

  public void run(){
    expensiveOperation();

    Message m = new Message();
    Bundle b = new Bundle();
    b.putString("message","result");
    m.setData(b);
    extractHandler.dispatchMessage(m);
    }
}

private Handler extractHandler = new Handler(){

  public void handleMessage(Message m){

    Bundle b = m.getData();
    String message = b.getString("message");

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(message)
        .setCancelable(false)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
              }
    });
    builder.create().show();
  }
};

仍然到达

01-07 11:55:02.791: ERROR/AndroidRuntime(18791): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

builder.create().show(); 行,尽管事实上这是在 Handler 内部调用的在我的主线程中 handleMessage 中。我做错了什么?

I'm sure I'm doing something stupid here, but the following code:

...

public void onClick(View v) {
  extractThread et = new extractThread();
  et.start();
}

...

private class extractThread extends Thread{

  public void run(){
    expensiveOperation();

    Message m = new Message();
    Bundle b = new Bundle();
    b.putString("message","result");
    m.setData(b);
    extractHandler.dispatchMessage(m);
    }
}

private Handler extractHandler = new Handler(){

  public void handleMessage(Message m){

    Bundle b = m.getData();
    String message = b.getString("message");

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(message)
        .setCancelable(false)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
              }
    });
    builder.create().show();
  }
};

is still getting

01-07 11:55:02.791: ERROR/AndroidRuntime(18791): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

at the line builder.create().show();, despite the fact this is called inside a Handler in my main thread inside handleMessage. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小糖芽 2024-10-18 11:07:20

您发布的代码是在 Activity 类中吗?

我也会使用

Message msg = Message.obtain();

而不是创建新消息。另外,如果您只想传递String,那么:

private static final int HANDLER_MESSAGE_RESULT = 0;
...
msg.what = HANDLER_MESSAGE_RESULT;
msg.obj = "result";

比传递Bundle更有效。在您的处理程序中,您可以切换内容,这允许您将来添加新的消息类型。我认为您还应该使用:

extractHandler.sendMessage(m);

而不是

extractHandler.dispatchMessage(m);

不确定是否可以解决您的问题!

Is the code you posted within your Activity class?

I would also use

Message msg = Message.obtain();

rather than creating a new message. Also if you just want to pass a String then:

private static final int HANDLER_MESSAGE_RESULT = 0;
...
msg.what = HANDLER_MESSAGE_RESULT;
msg.obj = "result";

is more efficient than passing a Bundle. In your Handler you can then switch on the what which allows you to add new message types in the future. I think you should also be using:

extractHandler.sendMessage(m);

rather than

extractHandler.dispatchMessage(m);

Not sure that any of that fixes your issue though!

や三分注定 2024-10-18 11:07:20

是的,这是一件愚蠢的事情。我应该使用 extractHandler.sendMessage(m); 而不是 extractHandler.dispatchMessage(m);

Yes, it was something stupid. I should have used extractHandler.sendMessage(m); instead of extractHandler.dispatchMessage(m);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文