2010-07-30 44 views
1

我有兩個AsyncTask活動,其唯一的責任是在UI線程上執行一個AsyncTask。其中一個很好,顯示progressBar,並更新ProgressBar消息。這是用於我的SearchActivity,它實際上是一個搜索活動,其中包含用於聲明搜索活動的Google搜索元數據。我創建了AsyncTask的克隆,並且只更改了doInBackground代碼,並且還克隆了SearchActivity並將其命名爲browseActivity。AsyncTask在預執行時不顯示

我開始BrowseActivity的結果在一個類擴展listView當一個項目被點擊。我打算髮生的事情是,AsyncTask的onPreExecute代碼在ListView活動和ListView將啓動onActivityResult的ResultActivity之間顯示progressAar,如SearchActivity所做的那樣。正在調用onPreExcute代碼,但ProgressBar從不顯示。 (我想我正確地運行了它在UI線程?)

這裏是BrowseActivity:

AsyncTask<String, String, ArrayList<SearchResult>> browseTask; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Intent intent = getIntent(); 
    String genreExtra = getIntent().getStringExtra(Genre.BUNDLE_TAG); 
     if(genreExtra!=null){ 
      genre = Genre.getgenreFromExtra(genreExtra); 
     } 

     String categoryExtra = getIntent().getStringExtra(Category.BUNDLE_TAG); 
     if(categoryExtra!=null){ 
      this.category = Category.getCategoryFromExtra(categoryExtra); 
     } 

     final Connector connector = GlobalVars.getCurrentConnector(); 

     Thread browseThread = new Thread(){ 

      @Override 
      public void run() { 
       try { 
        browseTask = connector.browse(BrowseActivity.this, category, genre, 1); 
        browseTask.execute(""); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (ExecutionException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

      }; 

      runOnUiThread(browseThread); 
      ArrayList<SearchResult> result = null; 

      try { 
       result = browseTask.get(); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ExecutionException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      Bundle queryBundle = new Bundle(); 
      queryBundle.putSerializable(SERIALIZABLE_KEY, result); 
      Intent i = new Intent(); 
      i.putExtra(BUNDLE_KEY, queryBundle); //Put in our browse results 
      i.putExtra(Category.BUNDLE_TAG, category.putExtra()); //put our category in return intent 
      i.putExtra(Genre.BUNDLE_TAG, genre.putExtra()); //put our genre in return intent 
      setResult(RESULT_OK, i); 
      finish(); 

} 

這裏是CategoryActivity(擴展ListActivity),其中browseActivity啓動活動:

@Override 
    protected void onListItemClick(ListView l, View v, final int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     Intent i = new Intent(CategorySelectionView.this, BrowseActivity.class); 
     final Category[] categories = Category.values(); 
     i.putExtra(Category.BUNDLE_TAG, categories[position].putExtra()); 
     i.putExtra(Genre.BUNDLE_TAG, genre.putExtra()); 
     CategorySelectionView.this.startActivityForResult(i, BrowseActivity.RESULT_REQUEST_CODE); 
    } 

// Listen for results. 
    protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
     // See which child activity is calling us back. 
     switch (requestCode) { 
      case BrowseActivity.RESULT_REQUEST_CODE: 
       // This is the standard resultCode that is sent back if the 
       // activity crashed or didn't doesn't supply an explicit result. 
       if (resultCode == RESULT_CANCELED){ 

       } 
       else { 
        ArrayList<SearchResult> recentQueryResults = (ArrayList<SearchResult>)data.getBundleExtra(
          BrowseActivity.BUNDLE_KEY).getSerializable(BrowseActivity.SERIALIZABLE_KEY); 
        GlobalVars.setRecentQueryResults(recentQueryResults); 
        Category category = Category.getCategoryFromExtra(data.getStringExtra(Category.BUNDLE_TAG)); 
        Intent i = new Intent(CategorySelectionView.this, RomListView.class); 
        i.putExtra(Category.BUNDLE_TAG, category.putExtra()); 
        i.putExtra(Genre.BUNDLE_TAG, genre.putExtra()); 
        startActivity(i); 
       } 
      default: 
       break; 
     } 
    } 

回答

1

首先,你不能在主/ UI線程上運行一個AsyncTask,這就是整個問題。當你打電話給browseTask.execute()它將在適當的Thread(主vs背景)上調用它的方法,而不管調用runOnUiThread()。通過調用runOnUiThread(),您正在做的所有事情都是在另一個Runnable中包裝啓動它的代碼。我想這不是你想要的,因爲在調用runOnUiThread()之後,你嘗試獲得尚未運行的任務的結果。 onCreate()中的代碼在UI線程上運行,因此必須先完成其他操作,即AsyncTaskonPreExecute()

你應該做的是創建AsyncTask並將runOnUiThread()之後的所有內容都放入onPostExecute()