简体   繁体   English

无法在未调用Looper.prepare()的线程内创建处理程序

[英]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.

相关问题 无法在尚未调用looper.prepare的线程内创建处理程序 - cant create handler inside thread that has not called looper.prepare 无法在未调用looper.prepare()的线程内创建处理程序 - cannot create handler inside thread that has not called looper.prepare() 无法在Handler()内未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside Handler() Android处理程序:无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android handler: Can't create handler inside thread that has not called Looper.prepare() Handler或runOnUiThread解决方案“无法在未调用Looper.prepare()的线程内创建处理程序” - Handler or runOnUiThread solution for “Can't create handler inside thread that has not called Looper.prepare() ” Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare() 无法在警告对话框线程中未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() on alert dialog thread 无法在 AsyncTask 内未调用 Looper.prepare() 的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask 无法在未调用Looper.prepare()的线程内创建处理程序 - 对话框中的AsyncTask - Can't create handler inside thread that has not called Looper.prepare() - AsyncTask inside a dialog 无法在未在AsyncTask for ProgressDialog中调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM