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表示ToastまたはAlert Dialogスレッドで使用します。

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

ワーカースレッドから呼び出しています。メインスレッド内からToast.makeText()またはAlertDialogを呼び出す必要があります。ハンドラーを使用することもできます。

于 2013-03-21T12:11:03.577 に答える
0
  1. You have to put Looper.prepare() inside the showDialog() method
  2. To open Wifi Settings Start a new Intent:

startActivity(new Intent(WifiManager.ACTION_WIFI_SETTINGS));

(alse see the android doc about this last point)

于 2013-03-21T12:24:07.423 に答える