首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Alertdialog不工作,在Android中出现异常

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

Stack Overflow用户
提问于 2013-03-21 20:09:22
回答 2查看 832关注 0票数 1

我尝试执行以下代码,但它产生了问题。没有这一点,代码就能正常工作。我希望弹出一个对话框,要求用户在关闭Wifi时启用它。

代码语言:javascript
运行
复制
@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代码也抛出了相同的错误!

代码语言:javascript
运行
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-21 20:11:04

使用runOnUiThread在线程中显示ToastAlert Dialog

代码语言:javascript
运行
复制
runOnUiThread(new Runnable() 
{
  @Override
  public void run() 
  {
     showAlert();          
  }
});

您正在从工作线程调用它。您需要从主线程中调用Toast.makeText()或Alert Dialog。您也可以使用处理程序。

票数 2
EN

Stack Overflow用户

发布于 2013-03-21 20:24:07

  1. 您必须将Looper.prepare()放入showDialog() method
  2. 中才能打开showDialog()设置开始新的意图:

startActivity(new Intent(WifiManager.ACTION_WIFI_SETTINGS));

(有关最后一点,请参阅android doc )

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15547124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档