Android onPreExecute无法显示ProgressDialog

时间:2013-07-31 07:52:09

标签: android android-asynctask

所以,我的AsyncTask如下:

private class AsyncRetriever extends AsyncTask<IotdHandler,Void,IotdHandler>{
        ProgressDialog pDialog;
        @Override
        protected void onPreExecute(){
            pDialog = new ProgressDialog(getApplicationContext());
            pDialog.setMessage(getResources().getString(R.string.toast_msg1));
            pDialog.show();
        }
//------------------------------------------------------------------------------  

这是MainActivity的内部类。但是,LogCat将pDialog.show()标记为错误 它说,Unable to start activity ComponentInfo.

我该如何解决?

2 个答案:

答案 0 :(得分:3)

ProgressDialog需要一个活动上下文。试试这个:

pDialog = new ProgressDialog(YourActivity.this);

答案 1 :(得分:1)

private class AsyncRetriever extends AsyncTask<IotdHandler,Void,IotdHandler>{
    ProgressDialog pDialog;
Context mcontext;
public AsyncRetriever(Context c){
   mcontext=c
}
    @Override
    protected void onPreExecute(){
        pDialog = new ProgressDialog(mcontext);
        pDialog.setMessage(getResources().getString(R.string.toast_msg1));
        pDialog.show();
    }

在此构造函数中传递调用类的上下文