Asynctask进度对话框

时间:2012-03-20 12:34:14

标签: android android-asynctask progressdialog

我将一些初始代码移动到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;

我真的很感激任何帮助。

2 个答案:

答案 0 :(得分:-1)

在AsyncTask类的外侧创建进度对话框对话框=新进度对话框(Home.this);

答案 1 :(得分:-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);