0

我似乎无法让它工作,在 progressDialog 完成显示后如何显示警报对话框?

这是一些相关的代码:

progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");

        new Thread() {
            public void run() {
                try{
                    initializer();
                } catch (Exception e) {
                    Log.e("tag", e.getMessage());
                }
                progressDialog.dismiss();
                SharedPreferences welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                welcome = welcome_pref.getString("getwelcome", "null");

                if (welcome.equals("no")) {

                }else{
                    AlertDialog welcome = new AlertDialog.Builder(MainActivity.this).show();
                    welcome.setContentView(R.layout.welcome);
                    welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                    SharedPreferences.Editor welc = welcome_pref.edit();
                    welc.putString("getwelcome", "no");
                    welc.commit();
                }
            }
        }.start();
    }

    private void initializer() {
        startMoving = (ImageView) findViewById(R.id.imMove);
        aboutApp = (ImageView) findViewById(R.id.imAbout);
        devSite = (ImageView) findViewById(R.id.imDevSite);
        movingTips = (ImageView) findViewById(R.id.imTips);
        rateApp = (ImageView) findViewById(R.id.imRate);

        start = (EditText) findViewById(R.id.etFrom);
        end = (EditText) findViewById(R.id.etTo);

        startMoving.setOnClickListener(this);
        aboutApp.setOnClickListener(this);
        devSite.setOnClickListener(this);
        movingTips.setOnClickListener(this);
        rateApp.setOnClickListener(this);

        tvabout = (TextView) findViewById(R.id.tvabout);
        tvstartmove = (TextView) findViewById(R.id.tvStart);
        tvrate = (TextView) findViewById(R.id.tvRate);
        tvdevelop = (TextView) findViewById(R.id.tvDev);
        tvmovetips = (TextView) findViewById(R.id.tvTips);

        tvabout.setShadowLayer(30, 0, 0, 0xff000000);
        tvstartmove.setShadowLayer(30, 0, 0, 0xff000000);
        tvrate.setShadowLayer(30, 0, 0, 0xff000000);
        tvdevelop.setShadowLayer(30, 0, 0, 0xff000000);
        tvmovetips.setShadowLayer(30, 0, 0, 0xff000000);

        checklistitem();

    }

    private void checklistitem() {
        SharedPreferences createlistitem_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        checkitem = createlistitem_pref.getString("getcheck", "null");

        if (checkitem.equals("no")) {

        }else{
            SQLHandler checkitemstat = new SQLHandler(MainActivity.this);
            checkitemstat.open();
            checkitemstat.createList();
            checkitemstat.close();
            SharedPreferences setitem_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
            SharedPreferences.Editor setcheck = setitem_pref.edit();
            setcheck.putString("getcheck", "no");
            setcheck.commit();
        }
    }
}

我收到一条错误消息,说Can't create handler inside thread that has not called Looper.prepare()我知道是什么导致了这个错误,但我不知道如何解决这个问题。

这行代码就是造成这种情况的原因。

                progressDialog.dismiss();
                SharedPreferences welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                welcome = welcome_pref.getString("getwelcome", "null");

                if (welcome.equals("no")) {

                }else{
                    AlertDialog welcome = new AlertDialog.Builder(MainActivity.this).show();
                    welcome.setContentView(R.layout.welcome);
                    welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                    SharedPreferences.Editor welc = welcome_pref.edit();
                    welc.putString("getwelcome", "no");
                    welc.commit();
                }
4

3 回答 3

1

在 runonUiThread 中添加警报对话框显示代码,

this.runOnUiThread(new Runnable() {

                public void run() {
                    // TODO Auto-generated method stub

                }
            });

像这样的东西,

}else{

 MainActivity.this.runOnUiThread(new Runnable() {

                public void run() {
                      AlertDialog welcome = new AlertDialog.Builder(MainActivity.this).show();
            welcome.setContentView(R.layout.welcome);
                }
            });


                    welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                    SharedPreferences.Editor welc = welcome_pref.edit();
                    welc.putString("getwelcome", "no");
                    welc.commit();
                }

正如此处建议的答案之一所说,您无法从工作线程更新您的 UI。当您更新任何 UI 时,您必须处于 UI 线程中。

因此,您需要使用处理程序或 runonUiThread 来执行此操作。但由于 runOnUiThread 在内部使用处理程序,它应该对您有所帮助。

于 2012-10-26T07:07:27.073 回答
1

您无法在线程内更新 UI。只有通过主线程才能更新 UI,您必须使用处理程序。否则你可以这样做

runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                     progressDialog.dismiss();
 AlertDialog welcome = new AlertDialog.Builder(MainActivity.this).show();
                    welcome.setContentView(R.layout.welcome);
                    welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                    SharedPreferences.Editor welc = welcome_pref.edit();
                    welc.putString("getwelcome", "no");
                    welc.commit();
                }
            });
于 2012-10-26T07:04:40.373 回答
0
SharedPreferences welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                welcome = welcome_pref.getString("getwelcome", "null");

                if (welcome.equals("no")) {

                }else{
                    AlertDialog welcome = new AlertDialog.Builder(MainActivity.this).show();
                    welcome.setContentView(R.layout.welcome);
                    welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                    SharedPreferences.Editor welc = welcome_pref.edit();
                    welc.putString("getwelcome", "no");
                    welc.commit();
                }

Thread.

于 2012-10-26T07:04:13.180 回答