在asynctask中正确设置进度对话框

时间:2014-05-01 07:08:43

标签: android android-asynctask progressdialog dialog

我目前正在尝试在Dialogbox的OnclickListener上显示进度对话框,因为我的项目从服务器获取的时间太长。   我按照此处的建议(Android progress dialog)和此帖子(android problem with progress dialog)使用Async任务来显示进度对话框显示进度对话框,但是当进入后台时,代码会返回异常" Looper没有设置"。当我设置looper没有任何反应。

我现阶段不确定我做错了什么。

public void firstMethod()
{
    final CustomObj obj = getCustomObj();//not imp

        Messages.getInstance().showAlert(MainActivity.this, "message", false, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();

            }
        }, new DialogInterface.OnClickListener()
        {

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                gotoAnotherPge(obj);

            }

        });


}   

public void gotoAnotherPge(final CustomObject obj)
{

    final ProgressDialog pd = new ProgressDialog(MainActivity.this);

     new AsyncTask<Object, Object, Boolean>()
    {

        protected void onPreExecute()
        {

            pd.setMessage(String.format(Statics.getText(MainActivity.this, R.raw.dictionary, "subscriptions_loading_unsubscribing")));
            pd.show();
        }

        protected Boolean doInBackground(Object... params)
        {       


            try{
                Looper.prepare();
                final LocalPopulator lp = new LocalPopulator(MainActivity.this, 0)
                {

                    @Override
                    public void populate()
                    {
                       List<Serializable> items = Arrays.asList(getItemHere(obj));

                        List<Serializable> listItems = new ArrayList<Serializable>();
                       listItems.addAll(items);
                        Serializable[] sItems = listItems.toArray(new Serializable[menuItems.size()]);
                        result = sItems;
                    }
                };
                showNextPage(true, 1, 0, lp);

                Looper.loop();
            }catch (Exception e){
                Log.e("tag", e.getMessage());
                /*
                 * The task failed
                 */
                return false;
            }

            return true;
        }
        protected void onPostExecute(Boolean result)
        {
            pd.dismiss();
        }


    };
    MainActivity.this.runOnUiThread (new Runnable()
    {
        @Override
        public void run()
        {

            // dismiss the progressdialog
            pd.dismiss();
        }
    });


}

0 个答案:

没有答案