Alertdialog不工作,它在Android中出现异常

Alertdialog不工作,它在Android中出现异常,android,runtime,android-alertdialog,Android,Runtime,Android Alertdialog,我试图执行下面的代码,但它产生了问题。没有这些,代码就可以正常工作。我想弹出一个对话框,要求用户在Wifi关闭时启用Wifi @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.welcome); Thr

我试图执行下面的代码,但它产生了问题。没有这些,代码就可以正常工作。我想弹出一个对话框,要求用户在Wifi关闭时启用Wifi

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);
    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(2000);                    
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            finally{                    
                showAlert();                    
            }
        }
    };
    timer.start();
}

}

public void showAlert() {

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

    // set title
    alertDialogBuilder.setTitle("Your Title");

    // set dialog message
    alertDialogBuilder
            .setMessage("Click yes to exit!")
            .setCancelable(false)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, close
                            // current activity
                            Welcome.this.finish();
                        }
                    })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();


        // show it
        alertDialog.show();
}
下面是日志历史记录,注释行中的ProgressDialog代码也会抛出相同的错误!:

11:46:57.306: E/AndroidRuntime(2140): FATAL EXCEPTION: Thread-141 
11:46:57.306: E/AndroidRuntime(2140): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11:46:57.306: E/AndroidRuntime(2140):   at android.os.Handler.<init>(Handler.java:197)
11:46:57.306: E/AndroidRuntime(2140):   at android.os.Handler.<init>(Handler.java:111)
11:46:57.306: E/AndroidRuntime(2140):   at android.app.Dialog.<init>(Dialog.java:107)
11:46:57.306: E/AndroidRuntime(2140):   at android.app.AlertDialog.<init>(AlertDialog.java:114)
11:46:57.306: E/AndroidRuntime(2140):   at android.app.AlertDialog$Builder.create(AlertDialog.java:931)
11:46:57.306: E/AndroidRuntime(2140):   at com.example.core4voipmobiledialer.Welcome.showAlert(Welcome.java:116)
11:46:57.306: E/AndroidRuntime(2140):   at com.example.core4voipmobiledialer.Welcome$1.run(Welcome.java:35)
11:46:57.306:E/AndroidRuntime(2140):致命异常:线程-141
11:46:57.306:E/AndroidRuntime(2140):java.lang.RuntimeException:无法在尚未调用Looper.prepare()的线程内创建处理程序
11:46:57.306:E/AndroidRuntime(2140):位于android.os.Handler.(Handler.java:197)
11:46:57.306:E/AndroidRuntime(2140):位于android.os.Handler.(Handler.java:111)
11:46:57.306:E/AndroidRuntime(2140):在android.app.Dialog(Dialog.java:107)上
11:46:57.306:E/AndroidRuntime(2140):在android.app.AlertDialog.(AlertDialog.java:114)
11:46:57.306:E/AndroidRuntime(2140):在android.app.AlertDialog$Builder.create(AlertDialog.java:931)
11:46:57.306:E/AndroidRuntime(2140):在com.example.core4voipmobiledialer.Welcome.showart(Welcome.java:116)上
11:46:57.306:E/AndroidRuntime(2140):在com.example.core4voipmobiledialer.Welcome$1.run(Welcome.java:35)

使用
runOnUiThread
在线程中显示
Toast
警报对话框

runOnUiThread(new Runnable() 
{
  @Override
  public void run() 
  {
     showAlert();          
  }
});
您正在从工作线程调用它。您需要在主线程中调用Toast.makeText()或警报对话框。您还可以使用处理程序

  • 您必须将
    Looper.prepare()
    放入
    showDialog()方法中
  • 要打开Wifi设置,请启动新的意图:
  • startActivity(新意图(WifiManager.ACTION\u WIFI\u设置))

    (另请参见关于最后一点的说明)