【问题标题】:unable to start activity ComponentInfo ExpandableListview无法启动活动 ComponentInfo ExpandableListview
【发布时间】:2014-06-06 03:24:46
【问题描述】:

我有一个可以从 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;
    }

}

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

提前致谢

【问题讨论】:

    标签: android android-asynctask expandablelistview


    【解决方案1】:

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

    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;
                }
            });
            }
    

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

    【讨论】:

      猜你喜欢
      • 2012-11-23
      • 1970-01-01
      • 2013-11-14
      • 2014-05-12
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多