1

我尝试执行以下代码,但它会产生问题。没有它,代码可以正常工作。我希望弹出一个对话框并要求用户在 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)
4

2 回答 2

2

用于runOnUiThread显示ToastAlert Dialog在线程中。

runOnUiThread(new Runnable() 
{
  @Override
  public void run() 
  {
     showAlert();          
  }
});

您从工作线程中调用它。您需要从主线程中调用 Toast.makeText() 或警报对话框。您也可以使用处理程序。

于 2013-03-21T12:11:03.577 回答
0
  1. 你必须把Looper.prepare()里面的showDialog()方法
  2. 要打开 Wifi 设置启动一个新的 Intent:

startActivity(new Intent(WifiManager.ACTION_WIFI_SETTINGS));

(另请参阅有关最后一点的android 文档)

于 2013-03-21T12:24:07.423 回答