2014-01-09 92 views
0

Asynctask中的onPostExecute方法未被調用,並且progressdialog未被取消。我也沒有得到任何例外。任何幫助,將不勝感激。
這裏是我的代碼無法關閉片段中的postexecute方法中的進度對話框

public class ParseAsync extends AsyncTask<String, Void, Void> 
{ 
    ProgressDialog pDialog = null; 
    @Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute(); 
     pDialog = ProgressDialog.show(getActivity(), "", "Loading.....", true, false); 
    } 

    @Override 
    protected Void doInBackground(String... params) 
    { 
     RSSFeedXMLParser parser = new RSSFeedXMLParser(); 
     String urls = params[0]; 
     parser.parsedata(urls); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) 
    { 
     pDialog.dismiss(); 
     listAdapter = new ListAdapter(getActivity()); 
     lvnewsfeed.setAdapter(listAdapter); 
    } 

} 

我打電話oncreateview方法的AsyncTask是這樣的:

new ParseAsync().execute("rssfeedurl"); 

回答

0

讓您ParseAsync

Context ctx; 

public ParseAsync (Context mContext) { 
    this.ctx = mContext; 
} 

的構造現在把這個CTX變量ProgressDialog一個Context參數。

@Override 
protected void onPreExecute() 
{ 
    super.onPreExecute(); 
    pDialog = ProgressDialog.show(ctx, "", "Loading.....", true, false); 
} 
+0

這工作,感謝傢伙 – Anirudh

-1

你爲了需要返回類型得到onPostExecute()到被調用。您可以將Void返回類型更改爲String並返回一個簡單的「完成」字符串。那樣確實被調用。

+0

錯誤。不是問題 – Raghunandan

+0

@Raghunandan你運行的是哪個Android版本? –

+0

這有什麼關係?請參考asynctask文檔 – Raghunandan

0

喜請有一條線

dialog = new ProgressDialog(context);

在異步任務添加這些行

public ParseAsync (YoursActivity activity) { 
     this.activity = activity; 
     dialog = new ProgressDialog(activity); 
    } 
+0

我使用片段而不是活動,我試過這個,但它不起作用。 public ParseAsync(Context context) \t \t { \t \t context = getActivity(); \t \t pDialog = new ProgressDialog(context); \t \t} – Anirudh

0

嘗試添加該你的課......

ParseAsync(String urlPass) { 
      url = urlPass; 
     } 

,並啓動你的AsyncTask像這樣..

new ParseAsync(urlTopass).execute(); 

對於防爆:

private class connectAsyncTask extends AsyncTask<Void, Void, String> { 
     private ProgressDialog progressDialog; 
     String url; 

     connectAsyncTask(String urlPass) { 
      url = urlPass; 
     } 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 
      progressDialog = new ProgressDialog(context); 
      progressDialog.setMessage("Fetching route, Please wait..."); 
      progressDialog.setIndeterminate(true); 
      progressDialog.show(); 
     } 

     @Override 
     protected String doInBackground(Void... params) { 
      JSONParser jParser = new JSONParser(); 
      String json = jParser.getJSONFromUrl(url); 
      return json; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      progressDialog.hide(); 
      if (result != null) { 
       drawPath(result); 
      } 
     } 
    }