我试图在处理程序中显示警告对话框,但我无法显示警报。你能帮我看一下这个bug吗?
Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
myDialog.show();
super.handleMessage(msg);
}
};
class myThread implements Runnable {
public void run() {
try {
myHandler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
on onCreate:
myDialog= new AlertDialog.Builder(this).create();
myDialog.setTitle("hi");
myDialog.setMessage("thanks");
myDialog.setButton("Next...",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
}
});
new Thread(new myThread()).start();
....
我定义了myHandler和myThread。然后在onCreate中,我定义了一个对话框。然后调用mythread运行。我想mythread会向myHandler发送一条消息。然后,Myhandler将触发对话框。逻辑有什么问题?感谢。
答案 0 :(得分:1)
像这样更新你的处理程序
Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
//Show dialog
myDialog.show();
}
else{
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"No message from Thread",
Toast.LENGTH_SHORT).show();
}
}
};
并在您的主题更新中
class myThread implements Runnable {
public void run() {
try {
Message msgObj = myHandler.obtainMessage();
Bundle b = new Bundle();
b.putString("message", msg);
msgObj.setData(b);
myHandler.sendMessage(msgObj);
} catch (Exception e) {
e.printStackTrace();
}
}