关于ExpandableListView,请教各路大神!!!

serlon_gencen 2011-04-06 09:09:04
写了一段关于ExpandableListView的代码,但是调试时出了空指针异常(代码中已经把出现异常的地方标记出来了),但是就是没找出来为什么,请大神们帮忙,大神们顺便帮忙看看这个控件的使用是否还有其他问题,代码如下:
package duan.textfour;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

public class Four extends Activity {
private ArrayList<String> group=new ArrayList<String>();
private ArrayList<ArrayList<HashMap<String,Object>>> childs=new ArrayList<ArrayList<HashMap<String,Object>>>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setGroup("论坛");
setChilds("论坛", "天涯论坛", R.drawable.icon);
setChilds("论坛", "猫扑论坛", R.drawable.icon);
ExpandableListView exlv= (ExpandableListView) findViewById(R.id.expandableListView1);
exlv.setAdapter(new MyAdapter());
}
public void setGroup(String grpStr){
group.add(grpStr);
}

public void setChilds(String grpStr,String name,Object log){
ArrayList<HashMap<String,Object>> child=new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map1=new HashMap<String,Object>();
HashMap<String,Object> map2=new HashMap<String,Object>();
HashMap<String,Object> map3=new HashMap<String,Object>();
map1.put("grpStr", grpStr);
map2.put("name", name);
map3.put("log", log);
child.add(map1);
child.add(map2);
child.add(map3);
childs.add(child);
}



class MyAdapter extends BaseExpandableListAdapter{

class ViewHolder {
TextView tv;
ImageView iv;
}

class ViewHolder2{
TextView tv;
}
LayoutInflater chInflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
LayoutInflater grpInflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childs.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
ViewHolder vh=new ViewHolder();
if(convertView==null){
convertView=chInflater.inflate(R.layout.child,null);
vh.iv=(ImageView) findViewById(R.id.childlog_imageView);
vh.tv=(TextView) findViewById(R.id.child_textView);
convertView.setTag(vh);
}
else{
vh=(ViewHolder) convertView.getTag();
}
vh.tv.setText((String)childs.get(groupPosition).get(childPosition).get("name"));
vh.iv.setBackgroundResource((Integer)childs.get(groupPosition).get(childPosition).get("log"));
return convertView;
}

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

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

@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return group.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
ViewHolder2 vh=new ViewHolder2();
if(convertView==null){
convertView=grpInflater.inflate(R.layout.group, null);
vh.tv=(TextView) findViewById(R.id.group_textview);
convertView.setTag(vh);
}
else{
vh=(ViewHolder2) convertView.getTag();
}
//在下面这段代码中出现空指针异常
vh.tv.setText(group.get(groupPosition));
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 false;
}
}
}
...全文
226 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
cjssssss 2011-05-31
  • 打赏
  • 举报
回复
看看顶了
serlon_gencen 2011-04-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 gre1390 的回复:]

package duan.textfour;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.V……
[/Quote]

setChilds哪个地方有问题?
只修改setChild该怎么修改?
youngwolf 2011-04-08
  • 打赏
  • 举报
回复
就改下面这一行应该就行了:
vh.tv=(TextView) convertView.findViewById(R.id.group_textview);
gre1390 2011-04-08
  • 打赏
  • 举报
回复
package duan.textfour;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

public class Four extends Activity {
private ArrayList<String> group = new ArrayList<String>();
private ArrayList<ArrayList<HashMap<String, Object>>> childs = new ArrayList<ArrayList<HashMap<String, Object>>>();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setGroup("论坛");
setChilds("论坛", "天涯论坛");
setChilds("论坛", "猫扑论坛");
ExpandableListView exlv = (ExpandableListView) findViewById(R.id.expandableListView1);
exlv.setAdapter(new MyAdapter());
}

public void setGroup(String grpStr) {
group.add(grpStr);
}

public void setChilds(String grpStr, String name) {
ArrayList<HashMap<String, Object>> child = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map1 = new HashMap<String, Object>();
HashMap<String, Object> map2 = new HashMap<String, Object>();
map1.put("grpStr", grpStr);
map2.put("name", name);
child.add(map1);
child.add(map2);
childs.add(child);
}

class MyAdapter extends BaseExpandableListAdapter {

class ViewHolder {
TextView tv;
ImageView iv;
}

class ViewHolder2 {
TextView tv;
}

LayoutInflater chInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
LayoutInflater grpInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childs.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
ViewHolder vh;
if (convertView == null) {
convertView = chInflater.inflate(R.layout.child, null);
vh = new ViewHolder();
vh.iv = (ImageView) convertView.findViewById(R.id.childlog_imageView);
vh.tv = (TextView) convertView.findViewById(R.id.child_textView);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
if(childPosition==0){
vh.tv.setText("天涯论坛");
vh.iv.setImageResource(R.drawable.icon);
}
if(childPosition==1){
vh.tv.setText("猫扑论坛");
vh.iv.setImageResource(R.drawable.icon);
}
return convertView;
}

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

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

@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return group.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
ViewHolder2 vh = new ViewHolder2();
if (convertView == null) {
convertView = grpInflater.inflate(R.layout.group, null);
vh.tv = (TextView) convertView.findViewById(R.id.group_textview);
convertView.setTag(vh);
} else {
vh = (ViewHolder2) convertView.getTag();
}
// 在下面这段代码中出现空指针异常
vh.tv.setText(group.get(groupPosition));
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 false;
}
}
}

这是帮你改过的代码,我试了,可以运行,你不妨试试。
出现问题还有个原因,就是你设置setChild方法的时候,出现了错误,你实际上并没有把“猫扑论坛”这一项设定进去,我的建议是你可以在getChildView方法中去设定,这样简便一些,没有你那么复杂且容易出错。
gre1390 2011-04-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yang79tao 的回复:]
就改下面这一行应该就行了:
vh.tv=(TextView) convertView.findViewById(R.id.group_textview);
[/Quote]
在getChildView和getGroupView这两个方法中,必须指明你的findViewById方法是在哪个View中,所以除了这个地方要改,另外一个地方也要先注明是在convertView中findView才行。
serlon_gencen 2011-04-07
  • 打赏
  • 举报
回复
还是有问题啊
serlon_gencen 2011-04-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yang79tao 的回复:]

Java code

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder2 vh=null;
if(……
[/Quote]

什么问题?
youngwolf 2011-04-07
  • 打赏
  • 举报
回复

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder2 vh=null;
if(convertView==null){
convertView=grpInflater.inflate(R.layout.group, null);
vh=new ViewHolder2();
convertView.setTag(vh);
}else{
vh=(ViewHolder2) convertView.getTag();
}

vh.tv=(TextView) findViewById(R.id.group_textview);
vh.tv.setText(group.get(groupPosition));
return convertView;
}
youngwolf 2011-04-07
  • 打赏
  • 举报
回复
哦,你是自定义adpater,那要再看看。
youngwolf 2011-04-07
  • 打赏
  • 举报
回复
这个控件只支持String类型,即:
ArrayList<ArrayList<HashMap<String,String>>>类似这样的。
serlon_gencen 2011-04-07
  • 打赏
  • 举报
回复
自己顶下,各路大神帮帮忙

80,361

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧