簡體   English   中英

無法在未調用Looper.prepare()的線程內創建處理程序

[英]Can not create handler inside thread that has not called Looper.prepare()

延遲一段時間后,我嘗試顯示警報對話框:

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();

啟動應用程序后。 需要顯示alertdialog時,該應用程序停止運行。 它顯示了問題:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM