2 hhhnihaohhh hhhnihaohhh 于 2015.07.02 22:29 提问

点击子菜单报错怎么回事

public class Main extends Activity {

int[] images;
String[] desc;
String[][] sonDesc;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //1.获取到视图主键
    ExpandableListView list=(ExpandableListView) findViewById(R.id.expandableListView1);
    //2.创建一个适配器BaseExpandableListAdapter
    BaseExpandableListAdapter adapter=new BaseExpandableListAdapter() {

        int [] images={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
        String[] desc={"漂亮的杯子","可爱的女孩","好喝的牛奶","美女...."};
        String[][] sonDesc={
                {"红杯子","绿杯子","黄杯子","紫杯子"},
                {"小女孩","好女孩","坏女孩 ","大女孩"},
                {"纯牛奶","酸奶","早餐奶","晚餐奶"},
                {"美女1","美女2","美女3"}
            };


        @Override
        public int getGroupCount() {
            //返回列表项的个数
            return images.length;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            //返回指定子列表项的个数
            return sonDesc[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
            return desc[groupPosition];
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return sonDesc[groupPosition][childPosition];
        }

        @Override
        public long getGroupId(int groupPosition) {
            //返回组的id
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            //返回子列表项的id
            return childPosition;
        }

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            //返回列表项显示的视图
            LinearLayout ll=new LinearLayout(Main.this);
            ll.setOrientation(0);
            ImageView image=new ImageView(Main.this);
            image.setLayoutParams(new LayoutParams(100,100));//设置图片大小
            image.setImageResource(images[groupPosition]);//设置图片

            TextView text=new TextView(Main.this);
            text.setText(desc[groupPosition]);//设置文本

            ll.addView(image);
            ll.addView(text);

            return ll;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            //返回子列表项显示的视图
            LinearLayout ll=new LinearLayout(Main.this);
            ll.setOrientation(0);

            TextView text=new TextView(Main.this);
            text.setTextColor(Color.CYAN);
            text.setText(sonDesc[groupPosition][childPosition]);

            ll.addView(text);

            return ll;
        }

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


    };

    //将list
    list.setAdapter(adapter);

    list.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
             String s=sonDesc[groupPosition][childPosition];

            Intent intent=new Intent(Main.this,chart.class);
            //intent.putExtra("name", s);
            startActivity(intent);
            return false;
        }
    });


}

}

3个回答

devmiao
devmiao   Ds   Rxr 2015.07.02 22:31

看下报什么错,是不是有变量为null,这个和界面是相关的,直接看不出来。仔细调试下。

danielinbiti
danielinbiti   Ds   Rxr 2015.07.02 22:53
 String[][] sonDesc;//这里定义的是全局的

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //1.获取到视图主键
    ExpandableListView list=(ExpandableListView) findViewById(R.id.expandableListView1);
    //2.创建一个适配器BaseExpandableListAdapter
    BaseExpandableListAdapter adapter=new BaseExpandableListAdapter() {

        int [] images={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
        String[] desc={"漂亮的杯子","可爱的女孩","好喝的牛奶","美女...."};
        String[][] sonDesc={                                                        //这里定义了局部的,全局的实际没初始化
                {"红杯子","绿杯子","黄杯子","紫杯子"},
                {"小女孩","好女孩","坏女孩 ","大女孩"},
                {"纯牛奶","酸奶","早餐奶","晚餐奶"},
                {"美女1","美女2","美女3"}
            };


                        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
             String s=sonDesc[groupPosition][childPosition];//这里sonDesc用的是全局的,
                        应该用String s=(String)adapter.getChild(groupPosition,childPosition)
danielinbiti
danielinbiti 回复hhhnihaohhh: 报错不是很明显嘛,chart这个activity有错误,textview强转成edittext,检查一下chart的oncreate,是不是组件搞错了,把textview弄成edittext了
2 年多之前 回复
hhhnihaohhh
hhhnihaohhh 我把数组全改成全局的也还是 报错
2 年多之前 回复
hhhnihaohhh
hhhnihaohhh   2015.07.02 23:31

图片说明

Csdn user default icon
上传中...
上传图片
插入图片