急需-点击子菜单报错怎么回事

问题描述

点击子菜单报错怎么回事

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

}

}

解决方案

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

解决方案二:

 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)

解决方案三:

时间: 2024-12-31 21:50:20

急需-点击子菜单报错怎么回事的相关文章

android-安卓 popwindown 点击事件 会报错 ?

问题描述 安卓 popwindown 点击事件 会报错 ? 在Activty 里面有几个fragment fragment 里面放的是listview listview 的adapter有一个图片 点击图片弹出popwindown popwindown 里面有四个按钮 然后我点击按钮 就报AndroidRuntime(3072): android.view.WindowManager$BadTokenException: Unable to add window -- token android

maven compile报错-eclipse中指定父pom的compile插件版本后子工程报错

问题描述 eclipse中指定父pom的compile插件版本后子工程报错 开发环境:eclipse mars,m2eclipse,maven3.3(external) parent pom中,Management配置compile插件,但是不指定版本, son pom中不配置compile-Plugin, 此时执行编译:发现使用的是compile plugin version = 3.1 问题开始: 此时修改 Management配置compile插件version= other,比如3.3

这个错误要怎么消 我用了多种方法消掉但点击按钮就报错

问题描述 这个错误要怎么消 我用了多种方法消掉但点击按钮就报错 消掉后报错的图 解决方案 view里本来是没有context的.你把MyView2的构造函数里面的参数context存到本地然后再用就行啦.附代码: //声明变量 private Context mContext; //然后在构造函数里 this.mContext = context; //然后用context的时候: mContext.startActivity(intent);

图片-激光推送过来的消息点击的时候报错

问题描述 激光推送过来的消息点击的时候报错 而且推送过来的消息很久才收到 是不是还有什么东西没有添加 解决方案 你看看权限是不是都加了,再看看是不是jar或者so没加

c++-C#中DllNotFoundException: BulletPhysics有这个报错怎么回事??

问题描述 C#中DllNotFoundException: BulletPhysics有这个报错怎么回事?? C#中 DllNotFoundException: BulletPhysics BulletPhysics._CreateWorld (IntPtr iValues, Int32 iValueLength, IntPtr fValues, Int32 fValueLength) (at Assets/Anim/Anim/Scripts/Internal/BulletPhysics.cs:

sqlserver子查询报错

  sqlserver跟oracle对比,如果涉及到的子查询,则相应的表名.字段就必须命名一个别名才能执行.主要有两种情况: 1.查询sql里面只有简单子查询不包含分组.求和等待 eg:elect * from (select com_id from company) 如此会报错,必须加上外层加上别名,里层的select * from company这里可以加别名,也可以不加正确:select * from ( select com_id from company) a 2.查询的sql里面包含

struts2 select标签获取action的集合属性值 动态生成下拉菜单 报错

问题描述 1.要获取集合list的Action类public class RoleAction extends ActionSupport {private Role role;public RoleService roleService;public String message;public List list;public static ActionContext ac = null;public String getMessage() {return message;}@Resource

点击视频通话时 报错

问题描述 java.lang.ExceptionInInitializerError at com.easemob.a.a.(Unknown Source) at com.easemob.a.a.a(Unknown Source) at com.easemob.chat.EMVideoCallHelper.setSurfaceView(Unknown Source) at com.xhbok.xyx.family.chat.ui.VideoCallActivity.onCreate(VideoC

我想在datagridview上加字段但程序来是报错怎么回事呀? (来者给分呀)在线等待帮忙给处理一下!!!

问题描述 usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;namespaceJYMaterMangerSystem{publicpartialclassForm2:Form{SqlDataAd