2014-11-22 99 views
0

我試圖從我的Parse數據庫中加載我的應用程序的類別。我可以通過將EditText視圖設置爲從數據庫下載的字符串ArrayList的值來返回結果。但是,當我在doInBackground方法中返回ArrayList,並嘗試將相同的EditText設置爲onPostExecute方法中的結果時,它說索引超出範圍,ArrayList大小爲0.這是我的代碼:AsyncTask onPostExecute不返回doInBackground的結果

private class DownloadCategories extends AsyncTask<Void, Void, ArrayList<String>> { 
    protected ArrayList<String> doInBackground(Void... voi) { 
     final ArrayList<String> load = new ArrayList<String>(); 
     ParseQuery<ParseObject> query = ParseQuery.getQuery("Categories"); 
     query.findInBackground(new FindCallback<ParseObject>() { 
      @SuppressLint("NewApi") 
      public void done(List<ParseObject> objects, ParseException e) { 
       String category; 
       if (e == null) { 
        for (int i = 0; i < objects.size(); i++) { 
         ParseObject pObject = objects.get(i); 
         category = pObject.getString("name"); 
         load.add(category); 

        } 
        // onSucced(objects); 
       } else { 

       } 
       //This is where i successfully can set the EditText to the first index. 
       address.setText(load.get(0)); 
      } 
     }); 
     return load; 
    } 

    protected void onPostExecute(ArrayList<String> loadedCats) { 

     //This is where it gives me a null pointer error, since loadedCats is empty. 
     address.setText(loadedCats.get(0)); 

    } 

} 

我應該只使用一個類變量訪問onPostExecute方法中的變量,或者只是在我下載了類別後從doInBackground方法更新UI,或者您是否有解決問題的結果?

在此先感謝。

+0

當然,它應該返回null,你在背景線程中調用後臺任務! – 2014-11-22 21:11:54

回答

1

你不需要父母AsyncTask,你已經在使用findInBackground

這裏導致問題的原因是您在後臺運行findInBackground,所以它的父代AsyncTask將不會等到調用done方法。相反,它返回空的ArrayList。之後,done方法被調用,所以它不會對列表產生任何影響。

相關問題