2012-03-20 147 views
1

我有一個問題,我的一些初始代碼移動到Asynctask中。Asynctask進度對話框

我一直在閱讀幾個Asynctask示例以及Asynctask上的Android開發人員文章,但是我發現很難將其與我能夠理解的代碼聯繫起來。

下面的代碼給我一個錯誤和執行doInBackground()時力關閉,是造成:了java.lang.RuntimeException:不能()內螺紋已不叫Looper.prepare創建處理程序

public class LoadingDialog extends AsyncTask<ArrayList<HashMap<String, String>>, Void, ArrayList<HashMap<String, String>>> 
{ 

    ProgressDialog pDialog = new ProgressDialog(Home.this); 


    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     pDialog = ProgressDialog.show(Home.this, "", "Loading..."); 

    } 

    protected ArrayList<HashMap<String, String>> doInBackground(ArrayList<HashMap<String, String>>... params) { 
     // TODO Auto-generated method stub 
     finalList.clear(); 
     eventList = database.getEvents(); 
     venueList = database.getVenues(); 
     finalList = createFinalList(eventList, venueList); 

     return finalList; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) { 
     // TODO Auto-generated method stub 
     pDialog.dismiss(); 

     ListAdapter adapter = new SimpleAdapter(Home.this, finalList , R.layout.home, 
        new String[] { "eventName", "venueName", "startTime"}, 
        new int[] { R.id.item_title, R.id.item_subtitle, R.id.item_datetime }); 
        setListAdapter(adapter); 

        lv = getListView(); 
        lv.setTextFilterEnabled(true); 

        lv.setOnItemClickListener(new OnItemClickListener() 
        { 
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
         { 
          Intent i = new Intent(Home.this, EventInfo.class); 

          HashMap<String, String> map = finalList.get(position); 
          String eID = map.get("eventId"); 
          String name = map.get("eventName"); 
          String venue = map.get("venueName"); 
          String lati = map.get("venueLatitude"); 
          String longi = map.get("venueLongitude"); 
          String opening = map.get("openingTimes"); 
          String descrip = map.get("venueDescription"); 
          String vID = map.get("venueId"); 
          String email = map.get("venueEmail"); 
          String phone = map.get("venuePhone"); 
          String facebook = map.get("venueFacebook"); 
          String twitter = map.get("venueTwitter"); 

          i.putExtra("itemId", eID); 
          i.putExtra("itemName", name); 
          i.putExtra("venueName", venue); 
          i.putExtra("venueLati", lati); 
          i.putExtra("venueLong", longi); 
          i.putExtra("opening", opening); 
          i.putExtra("venueDescription", descrip); 
          i.putExtra("venueID", vID); 
          i.putExtra("venueEmail", email); 
          i.putExtra("venuePhone", phone); 
          i.putExtra("venueFacebook", facebook); 
          i.putExtra("venueTwitter", twitter); 
          startActivityForResult(i, 0);    

         } 
        }); 

    } 


} 

這是家庭類的一部分:

public class Home extends ListActivity { 
int eventid; 
String[] venue = null; 
String[] name = null; 
private SqlParser database = new SqlParser(); 
private ArrayList<HashMap<String, String>> eventList = new ArrayList<HashMap<String, String>>(); 
private ArrayList<HashMap<String, String>> venueList = new ArrayList<HashMap<String, String>>(); 
private ArrayList<HashMap<String, String>> finalList = new ArrayList<HashMap<String, String>>(); 
ListView lv = null; 

我真的很感激任何幫助。

+0

什麼'createFinalList'嗎?根據你的描述,錯誤來自那裏或從你的數據庫 – njzk2 2012-03-20 12:48:41

回答

-1

創建此行ProgressDialog pDialog = new ProgressDialog(Home.this);在出側AsynTask類

+0

ProgressDialog的創建是在onPreExecute,這是做它的正確方法。 – njzk2 2012-03-20 12:47:02

+0

然後這行是不重要ProgressDialog pDialog = new ProgressDialog(Home.this); – 2012-03-20 12:49:19

+0

事實上它不是 – njzk2 2012-03-20 12:49:48

-1

只是檢查你的代碼:

@Override 
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) { 
     // TODO Auto-generated method stub 
     pDialog.dismiss(); 

     ListAdapter adapter = new SimpleAdapter(Home.this, finalList , R.layout.home, 
        new String[] { "eventName", "venueName", "startTime"}, 
        new int[] { R.id.item_title, R.id.item_subtitle, R.id.item_datetime }); 
        setListAdapter(adapter); 

        lv = getListView(); 

在這裏,您正試圖設置適配器獲得的ListView其實之前的ListView。

應該是這樣:

lv = getListView(); 
setListAdapter(adapter); 
+0

這兩行之間沒有關係。 – njzk2 2012-03-20 12:48:11

+0

@ njzk2你確定嗎?讓我如何能夠在獲得ListView之前設置適配器。 – 2012-03-20 12:54:10

+0

我很確定,是的。 getListView返回一個ListView。 setListAdapter是ListActivity類的一個方法。超級不知道特定的lv變量。 – njzk2 2012-03-20 13:12:51