[英]Can not create handler inside thread that has not called Looper.prepare()
I tried to show an alert dialog after some delay: 延迟一段时间后,我尝试显示警报对话框:
Thread thread = new Thread() {
@Override
public void run() {
try {
synchronized (this) {
wait(3000);
}
} catch (InterruptedException ex) {
}
final AlertDialog.Builder builder = new AlertDialog.Builder(stop_watch.fa);
builder.setMessage("Want to exit?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
locator_activity.fa.finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
};
thread.start();
After launching app. 启动应用程序后。 When it's time to show alertdialog the app stopped working.
需要显示alertdialog时,该应用程序停止运行。 And it shows problem:-
它显示了问题:
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
AlertDialogs are UI elements and so the work needs to be executed on the UI Thread: AlertDialogs是UI元素,因此需要在UI线程上执行工作:
new Thread() {
public void run() {
activity.this.runOnUiThread(new Runnable() {
public void run() {
// Show dialog..
}
});
}
}.start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.