Android 处理程序问题
我确信我在这里做了一些愚蠢的事情,但是以下代码:
...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您发布的代码是在
Activity
类中吗?我也会使用
而不是创建新消息。另外,如果您只想传递
String
,那么:比传递
Bundle
更有效。在您的处理程序
中,您可以切换
内容
,这允许您将来添加新的消息类型。我认为您还应该使用:而不是
不确定是否可以解决您的问题!
Is the code you posted within your
Activity
class?I would also use
rather than creating a new message. Also if you just want to pass a
String
then:is more efficient than passing a
Bundle
. In yourHandler
you can thenswitch
on thewhat
which allows you to add new message types in the future. I think you should also be using:rather than
Not sure that any of that fixes your issue though!
是的,这是一件愚蠢的事情。我应该使用
extractHandler.sendMessage(m);
而不是extractHandler.dispatchMessage(m);
Yes, it was something stupid. I should have used
extractHandler.sendMessage(m);
instead ofextractHandler.dispatchMessage(m);