【问题标题】:Can't create handler inside thread that has not called Looper.prepare()-Alertdialogbox [duplicate]无法在未调用 Looper.prepare()-Alertdialogbox 的线程内创建处理程序 [重复]
【发布时间】:2012-11-27 19:56:38
【问题描述】:

可能重复:
Can’t create handler inside thread that has not called Looper.prepare()

我想在线程中调用alertdialogbox如下

    public class connect implements Runnable
     {
     public void run()  //run method

    {

        AlertDialog.Builder alert = new AlertDialog.Builder(cordovaExample.activity);

        alert.setTitle("Confirm");
        alert.setMessage("Are you sure?");

        alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // Do nothing but close the dialog

                dialog.dismiss();
            }

        });

        alert.setNegativeButton("NO", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // Do nothing
                dialog.dismiss();
            }
        });

        AlertDialog s = alert.create();
        alert.show();
       }
     }

我从以下活动中调用此线程

public class cordovaExample extends DroidGap
{
    Context mcontext;

    public static cordovaExample activity;
private static MediaPlayer  music=null;
    @Override
    public void onCreate(Bundle savedInstanceState)

        {
            super.onCreate(savedInstanceState);

            activity=this;

                             super.init();



                   new Thread(new connect(this)).start();

                     }
                }

但它给出错误 无法在未调用 Looper.prepare() 的线程内创建处理程序

任何帮助将不胜感激

【问题讨论】:

    标签: android cordova


    【解决方案1】:

    在App UI线程中写下这一行

     alert.show();
    

    类似的东西

    MainActivity.this.runOnUiThread(new Runnable() {
    
            public void run() {
                  alert.show();
    
            }
        });
    

    【讨论】:

      【解决方案2】:

      你不能在线程运行时对 ui 进行更改。如果你想这样做,请使用 Handler 或 runOnUiThead 或最好的选择是使用 AsyncTask。

      【讨论】:

        猜你喜欢
        • 2012-10-01
        • 1970-01-01
        • 2012-08-15
        • 2011-09-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多