2011-04-05 133 views
0

我有一個進度對話框類,它是我主要活動的一個子類。它本身運行良好。Android異步進度對話框延遲

public class UpdateFeedTask extends AsyncTask<Void, Void, Void> { 

    ProgressDialog loading; 
    @Override 
    protected void onPreExecute() { 
     loading = ProgressDialog.show(NewsFeedActivity.this,"Please wait...", "Retrieving data ...", true); 
    } 

    @Override 
    protected void onProgressUpdate(Void... progress) { 

    } 


    @Override 
    protected void onPostExecute(Void result) { 

     loading.dismiss(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

} 

我有一個名爲getNews的方法,它發送一個http post請求並解析結果,這需要幾秒鐘的時間。在這種方法中,我調用UpdateFeedTask來異步顯示進度對話框,但它似乎不是同時執行的。進度對話框似乎只在http請求完成後纔打開,所以不是顯示進度對話框,而是發送請求,它似乎先完成方法getNews,然後在瞬間顯示進度對話框。在獲取數據之前,如何使對話框顯示?這裏是我所說的進度對話框

public void getNews(){ 

      //show the progress dialog 
    new UpdateFeedTask().execute(); 


    //go fetch some data 
    WheruClient newsFeedLocation = new WheruClient(); 

    try { 

     newsFeedArray = newsFeedLocation.getLocationActivity(sessionKey,page,filter,when); 


    }catch (ClientProtocolException e) { 
     Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show(); 
    } catch (IOException e) { 
     Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show(); 
    }catch (JSONException e) { 
     Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show(); 
    } 
} 

**編輯新的異步類發送HTTP請求在後臺,再返回JSON數組,但我得到一個致命的錯誤,上面寫着

04-07 15:33 :02.967:ERROR/AndroidRuntime(11004):引起:java.lang.RuntimeException:無法在未調用Looper.prepare()的線程中創建處理程序

我讀過你的例子,我想我錯過了一些關於在後臺工作,有什麼想法?

public class UpdateFeedTask extends AsyncTask<Void, Void, JSONArray> { 


    @Override 
    protected void onPreExecute() { 
     //loading = ProgressDialog.show(NewsFeedActivity.this,"Please wait...", "Retrieving data ...", true); 
    } 

    @Override 
    protected void onProgressUpdate(Void... progress) { 

    } 


    @Override 
    protected void onPostExecute(JSONArray result) { 

     //loading.dismiss(); 
     updateFeed(result); 
    } 

    @Override 
    protected JSONArray doInBackground(Void... params) { 

     WheruClient newsFeedLocation = new WheruClient(); 

     try { 
      newsFeedArray = newsFeedLocation.getLocationActivity(sessionKey,page,filter,when); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return newsFeedArray; 
    } 

} 

回答

1

需要在doInBackground中調用時間密集型任務。我在doInBackground方法中看不到任何東西。請注意,不要觸摸doInBackground中的用戶界面。我有一些示例代碼here.

+0

謝謝,我改變了結構,以執行doInBackground方法中的工作。它現在似乎打開進度對話框,去獲取JSON數組,但之後迅速崩潰。不太清楚問題是什麼。 – Brian 2011-04-07 22:42:21

+0

@Brian Perin也許doInBackground中的調用在該線程中拋出異常。我會將doInBackground中的調用包裝成try catch e和Log.d(TAG,「msg」,e),以查看應用程序崩潰的原因。 – JAL 2011-04-07 22:54:51