问题描述
- 点击子菜单报错怎么回事
-
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