无法启动活动ComponentInfo ExpandableListview

时间:2014-06-06 03:24:46

标签: android android-asynctask expandablelistview

我有一个expandablelistview,它从mysql中检索数据,所以我使用JSON和asynctask。 asynctask类也在主活动类中。当我运行程序时,我收到了这个错误。

这是我的主要课程:

new ProsesTampil().execute(url);
        exProteinAdapter = new com.ta.helper.ExpandableListAdapterKonsumsi(
                this, listDataHeaderProtein, listDataChildProtein);
        exProtein.setAdapter(exProteinAdapter);

        exProtein.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                group = groupPosition;
                child = childPosition;


                String kaloriString = listTakaranKaloriChildProtein.get(
                        listDataHeaderProtein.get(groupPosition)).get(
                        childPosition);
                kalori = Float.parseFloat(kaloriString);
                kolomKalori.setText("" + kalori);


                String namaMakanan = listDataChildProtein.get(
                        listDataHeaderProtein.get(groupPosition)).get(
                        childPosition);
                kolomNamaMakanan.setText(namaMakanan);

                return true;
            }
        });

class ProsesTampil extends AsyncTask<String, Void, JSONObject> {

        @Override
        protected JSONObject doInBackground(String... params) {
            return JSONfunctions.getJSONfromURL(params[0]);
        }

        @Override
        protected void onPostExecute(JSONObject result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            exProtein = (ExpandableListView) findViewById(R.id.EVprotein);
            exProtein.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
            listDataHeaderProtein = new ArrayList<String>();
            Collections.addAll(listDataHeaderProtein, getResources()
                    .getStringArray(R.array.input_menuP));

            List<String> daftarMakanan = new ArrayList<String>();
            List<String> takaranKaloriDaftarMakanan = new ArrayList<String>();
            //System.out.println(result);   
            try {
                JSONArray makanan = result.getJSONArray("food");
                for (int i = 0; i < makanan.length(); i++) {

                    // nama
                    Collections.addAll(daftarMakanan, makanan.getJSONObject(i)
                            .getString("nama_makanan").toString());
                    listDataChildProtein = new HashMap<String, List<String>>();
                    listDataChildProtein.put(listDataHeaderProtein.get(0),
                            daftarMakanan);

                    System.out.println(listDataChildProtein);

                    // kalori
                    Collections.addAll(takaranKaloriDaftarMakanan, makanan
                            .getJSONObject(i).getString("energi"));
                    listTakaranKaloriChildProtein = new HashMap<String, List<String>>();
                    listTakaranKaloriChildProtein.put(listDataHeaderProtein.get(0),
                            takaranKaloriDaftarMakanan);
                    System.out.println(listTakaranKaloriChildProtein);

                }
            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString());
            }
        }

适配器类

public ExpandableListAdapterKonsumsi(Context context,
            List<String> listDataHeader,
            HashMap<String, List<String>> listDataChild) {
        super();
        this.context = context;
        this.listDataHeader = listDataHeader;
        this.listDataChild = listDataChild;

    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return this.listDataChild.get(this.listDataHeader.get(groupPosition))
                .get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final String childText = (String) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(
                    R.layout.konsumsi_user_expand_listchild, null);
        }
        CheckedTextView listChild = (CheckedTextView) convertView
                .findViewById(R.id.lblListItemKonsumsi);
        listChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return this.listDataChild.get(this.listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return this.listDataHeader.get(groupPosition);
    }



    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return this.listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(
                    R.layout.daftar_akun_expand_listgroup, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

在尝试使用onPostExecute中的system.out.println时检索正确的数据,但似乎主程序无法识别它(null)。

提前致谢

1 个答案:

答案 0 :(得分:0)

好的,所以我想出了答案。解决方案是像这样修改主类

new ProsesTampil().execute(url);


class ProsesTampil extends AsyncTask<String, Void, JSONObject> {

        @Override
        protected JSONObject doInBackground(String... params) {
            return JSONfunctions.getJSONfromURL(params[0]);
        }

        @Override
        protected void onPostExecute(JSONObject result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            exProtein = (ExpandableListView) findViewById(R.id.EVprotein);
            exProtein.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
            listDataHeaderProtein = new ArrayList<String>();
            Collections.addAll(listDataHeaderProtein, getResources()
                    .getStringArray(R.array.input_menuP));

            List<String> daftarMakanan = new ArrayList<String>();
            List<String> takaranKaloriDaftarMakanan = new ArrayList<String>();
            //System.out.println(result);   
            try {
                JSONArray makanan = result.getJSONArray("food");
                for (int i = 0; i < makanan.length(); i++) {

                    // nama
                    Collections.addAll(daftarMakanan, makanan.getJSONObject(i)
                            .getString("nama_makanan").toString());
                    listDataChildProtein = new HashMap<String, List<String>>();
                    listDataChildProtein.put(listDataHeaderProtein.get(0),
                            daftarMakanan);

                    System.out.println(listDataChildProtein);

                    // kalori
                    Collections.addAll(takaranKaloriDaftarMakanan, makanan
                            .getJSONObject(i).getString("energi"));
                    listTakaranKaloriChildProtein = new HashMap<String, List<String>>();
                    listTakaranKaloriChildProtein.put(listDataHeaderProtein.get(0),
                            takaranKaloriDaftarMakanan);
                    System.out.println(listTakaranKaloriChildProtein);

                }
            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString());
            }
exProteinAdapter = new com.ta.helper.ExpandableListAdapterKonsumsi(
                MainActivity.this, listDataHeaderProtein, listDataChildProtein);
        exProtein.setAdapter(exProteinAdapter);

        exProtein.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                group = groupPosition;
                child = childPosition;


                String kaloriString = listTakaranKaloriChildProtein.get(
                        listDataHeaderProtein.get(groupPosition)).get(
                        childPosition);
                kalori = Float.parseFloat(kaloriString);
                kolomKalori.setText("" + kalori);


                String namaMakanan = listDataChildProtein.get(
                        listDataHeaderProtein.get(groupPosition)).get(
                        childPosition);
                kolomNamaMakanan.setText(namaMakanan);

                return true;
            }
        });
        }

希望这对有同样问题的其他用户有帮助