带线程的进度对话框

时间:2011-05-10 09:54:36

标签: android multithreading progressdialog

我正在研究android中的列表视图控件。当我点击列表视图项目时,我想要一个进度对话框,并在请求数据到来之后我想停止它

好吧我把这个对话框放在线程中,当线程停止时我想显示另一个对话框,但它实际上它不起作用它触发了下面给出的错误

05-10 15:05:10.607:ERROR / AndroidRuntime(820):未捕获的处理程序:线程MagentoBackground因未捕获的异常而退出 ** 05-10 15:05:10.617:ERROR / AndroidRuntime(820):java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序

我的代码就像这样

           Runnable viewQuestions = new Runnable(){
            public void run() {
                getViewAnswer(answerBean);
            }


        };
       thread =  new Thread(null, viewQuestions, "MagentoBackground");
       thread.start();
       m_ProgressDialog = ProgressDialog.show(context, "Please wait...", "Retrieving data ...", true);



      protected void getViewAnswer(AnswerBean answerBean) {
          //fetch data code
            if(viewAnswerBeans != null && viewAnswerBeans.size() > 0){
             m_ProgressDialog.dismiss();
             viewAndGiveAnswerDialog(viewAnswerBeans);
                             dialogbox.show();

         } else {
             m_ProgressDialog.dismiss();
                     dialogBox.show();

         }
      }

我的问题是这个“dialogBox.show();”不会工作它不会显示消息

任何人都可以帮我解决这个问题

6 个答案:

答案 0 :(得分:0)

这是因为您从非UI线程调用UI组件。看看以下链接

http://developer.android.com/resources/articles/painless-threading.html

答案 1 :(得分:0)

使用处理程序

private Handler gps_ws_handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
            m_ProgressDialog.dismiss();
            dialogBox.show();


    }
};

答案 2 :(得分:0)

在您的活动中创建一个处理程序。

Handler handler = new Handler();

然后在getViewAnswer中,向其发布消息。

protected void getViewAnswer(AnswerBean answerBean) {

    //fetch data code


    handler.post(new Runnable() {
    @Override
    public void run() {
        if(viewAnswerBeans != null && viewAnswerBeans.size() > 0){
        m_ProgressDialog.dismiss();
        viewAndGiveAnswerDialog(viewAnswerBeans);
                     dialogbox.show();

         } else {
        m_ProgressDialog.dismiss();
             dialogBox.show();

         }
    }
});
  }

答案 3 :(得分:0)

对于后台任务,使用带有ProgressDialog的AsyncTask,就像显示here一样。

答案 4 :(得分:0)

出于这些目的,我建议您使用AsyncTask

答案 5 :(得分:0)

试试这个

公共类YourActivity扩展了Activity {

private ProgressDialog m_ProgressDialog = null;
private Runnable viewOrders;
private ListView mListView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourLayout);

    viewOrders = new Runnable() {

        public void run() {
            loadList();
        }
    };

    Thread thread = new Thread(null, viewOrders, "MagentoBackground");
    thread.start();
    m_ProgressDialog = ProgressDialog.show(YourActivity.this, "",
            "Get Data...", true);

}

private Runnable returnRes = new Runnable() {

    public void run() {



            mListView = (ListView) findViewById(R.id.yourList);

            //create here your adapter 

            mListView.setAdapter(yourAdapter);
            mListView.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View v,
                        int position, long arg3) {

                    //on item click
                }
            });
        }
        m_ProgressDialog.dismiss();
    }
};

private void loadList() {
    try {
        /* Make your request Here
                    The progress Dialog is dismissed automatically after request is  finished */
        }

        Thread.sleep(1000);

    } catch (Exception e) {
    }

    runOnUiThread(returnRes);
}

}