在activity中设置ExpandableListView时出错

时间:2012-08-08 09:07:01

标签: android android-listview nullpointerexception

    public class Official_Activity extends Activity{
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.official_data2);
    setupViewComponent();

    new setList().execute();
}

 private void setupViewComponent() {
    // TODO Auto-generated method stub
     ExpandableListView list = (ExpandableListView)findViewById(R.id.list);
}

private class setList extends AsyncTask <Void, Void, List<News>>{

    List<Map<String, Object>> groups;
    List<List<HashMap<String, Object>>> childs;




     final ProgressDialog progDlg3 = new ProgressDialog(Official_Group.group);


    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        progDlg3.setTitle("Please wait");
        progDlg3.setMessage("Loading...");
        progDlg3.setIcon(android.R.drawable.ic_dialog_info);
        progDlg3.setCancelable(false);
        progDlg3.show();
    }

    @Override
    protected List<News> doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        List<News> newes = null;
        try {
            newes = GetJson.update();
            return newes;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return newes;


    }

    protected void onPostExecute(List<News> result){

         groups = new ArrayList<Map<String, Object>>();
        Map<String, Object> group1 = new HashMap<String, Object>();
        group1.put("ItemTitle", "Earthquake report");
        group1.put("ItemText", "Data from USGS");
        Map<String, Object> group2 = new HashMap<String, Object>();
        group2.put("ItemTitle", "Weather info");
        group2.put("ItemText", "Show weather info");
    groups.add(group1);
    groups.add(group2);

    List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
    for(News news : result){
        HashMap<String, Object> item = new HashMap<String, Object>();
        item.put("ItemTitle", news.getPlace());
        item.put("ItemText", "Magnitude: "+news.getMag());
        item.put("latit", news.getLatit());
        item.put("longit", news.getLongit());
        item.put("date", news.getTime());
        item.put("tzone", news.getTzone());
        data.add(item);
    }

List<HashMap<String, Object>> weather = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> item2 = new HashMap<String, Object>();
    item2.put("ItemTitle", "Coming Soon");
    item2.put("ItemText", "");
    item2.put("latit", "");
    item2.put("longit", "");
    item2.put("date", "");
    item2.put("tzone", "");
    weather.add(item2);

     childs = new ArrayList<List<HashMap<String, Object>>>();
    childs.add(data);
    childs.add(weather);

    ExpandableListAdapter mExpaListAdap = new SimpleExpandableListAdapter(
            this,
            groups,
            R.layout.list_item,
            new String[] {"ItemTitle", "ItemText"},                   
            new int[] {R.id.ItemTitle, R.id.ItemText},
            childs,                                     
            R.layout.list_earthquake,
            new String[] {"ItemTitle", "ItemText", "latit", "longit", "date", "tzone"},
            new int[] {R.id.ItemTitle, R.id.ItemText, R.id.latit, R.id.longit, R.id.date, R.id.tzone}
            );

        list.setAdapter(mExpaListAdap);
      progDlg3.dismiss();     
    }



 }
   }

但发现错误:

The constructor SimpleExpandableListAdapter(Official_Activity.setList,
List<Map<String,Object>>, int, String[], int[], List<List<HashMap<String,Object>>>,
int, String[], int[]) is undefined

如何解决?

getJson的代码:Intent with ListView (JSON data)

9/8更新: 在我将“this”更改为“Official_Activity.this”后,又发现了另一个错误:

  

08-08 09:34:33.975:E / AndroidRuntime(335):java.lang.RuntimeException:无法启动活动ComponentInfo {com.android.abc/com.android.abc.Official_Activity}:java.lang。的NullPointerException

代码中是否有错误?

1 个答案:

答案 0 :(得分:1)

你是一个内在的类,所以“this”是Official_Activity.setList class,而不是context,你必须写Official_Activity.this,如下:

new SimpleExpandableListAdapter(
            Official_Activity.this,
            groups,
            R.layout.list_item,
            new String[] {"ItemTitle", "ItemText"},                   
            new int[] {R.id.ItemTitle, R.id.ItemText},
            childs,                                     
            R.layout.list_earthquake,
            new String[] {"ItemTitle", "ItemText", "latit", "longit", "date", "tzone"},
            new int[] {R.id.ItemTitle, R.id.ItemText, R.id.latit, R.id.longit, R.id.date, R.id.tzone}
            );