异步进程对话框未显示在android中的应用程序中

时间:2010-11-30 11:54:05

标签: android

异步进程对话框未在android中的应用程序中显示。谁能告诉我这是什么问题?

我的代码

public void onClick(View v)
{
  new FetchTask(getBaseContext()).execute(null); 
}

 public class FetchTask extends AsyncTask<Object, ArrayList,ArrayList> 
 { 

     private ProgressDialog dlg; 
     private Context ctx; 

     public FetchTask(Context context) { 
         ctx = context; 
     } 

     @Override 
     protected void onPreExecute() { 
         dlg = new ProgressDialog(ctx); 
         dlg.setMessage("Loading...."); 
         dlg.show();
         super.onPreExecute(); 
     } 

     @Override 
     protected void onPostExecute(ArrayList parserValue) { 
         dlg.dismiss(); 

           for(int i=0;i<parservalue.size();i++)
             {
                DomParserDataSet dataset=(DomParserDataSet)parservalue.get(i);  
                 if(dataset.getToken_string().length()>1)
                      {


                  Intent homeIntent=new Intent(ctx,HelloTabWidget.class);
                  homeIntent.putExtra("strUserName", username);
                  homeIntent.putExtra("strPassword",password );
                  homeIntent.putExtra("questGroupId",dataset.getQuest_group_id());
                  homeIntent.putExtra("userid",dataset.getUser_id());
                  homeIntent.putExtra("systemuserid", dataset.getSystem_user_id());
                  homeIntent.putExtra("providerid",dataset.getIndex_provider_id());
                       startActivity(homeIntent);
                   //startActivity(new Intent(this,HelloTabWidget.class));

                   }
                   else
                   {            

                     DomParserDataSet dataset1=(DomParserDataSet)parservalue.get(i);   
                     AlertDialog.Builder alertbox = new AlertDialog.Builder(ctx);
                      alertbox.setMessage(dataset1.getError_value());
                         alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {


                           public void onClick(DialogInterface dialog, int arg1) {
                               // the button was clicked

                            dialog.cancel();

                           }
                       });

                       // show it
                       alertbox.show();       
                   }

             } 

             // display data 
         }

  @Override
  protected ArrayList doInBackground(Object... arg0) {       

   try
      {
          username=edtUserName.getText().toString();
          password=edtPassword.getText().toString();
          WebService service=new WebService();
          String webservicevalue=service.callServer(SOAP_ACTION,"", username,password,URL);
            webservicevalue=decodeXMLData(webservicevalue);
            DomParser xmlParser=new DomParser();
           parservalue=xmlParser.parseXmlFile(webservicevalue);

           String parservalue1=Integer.toString(parservalue.size());

           Log.d("parser value size is",parservalue1);


            }
             catch (Exception e) {
       // TODO: handle exception
              e.printStackTrace();
      }          

             return parservalue;
       } 

 } 

有人可以告诉我我要改变什么吗?

2 个答案:

答案 0 :(得分:1)

正如Arhimed所指出的,这是不正确的。留下未来参考的答案。

<击> 我认为你需要将super.onPreExecute();移到onPreExecute()的第一行,如下所示:

 @Override 
 protected void onPreExecute() { 
     super.onPreExecute();
     dlg = new ProgressDialog(ctx); 
     dlg.setMessage("Loading...."); 
     dlg.show(); 
 }

<击>

答案 1 :(得分:1)

好的,除了Nailuj之外,还有另外几个合格的猜测

  1. 可能是ProgressDialog构造函数需要一个包装Activity的实例作为上下文而不是Activity.getBaseContext()(BTW,我希望在{{1}上调用getBaseContext()实例(?))?
  2. 可能Activity运行得如此之快(可能会发生一些错误,请参阅LogCat所说的),FetchTask.doInBackground()只是没有时间出现?