ExpandableListAdpater问题
重写public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) 我返回一个TextView类型的变量,为什么会出现下面的错误啊:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams。TextView是View的子类,返回子类在语法上没问题啊,怎么回事呢忘大家指点。 --------------------编程问答-------------------- 需要return的View是convertView好嘛...... --------------------编程问答-------------------- 你这里不能直接返回textView,应用利用convertView.
if(convertView == null) {--------------------编程问答-------------------- API没有说一定要返回convertView啊,当然重用convertView是最好了,但不用convertView又不是不对啊。 --------------------编程问答--------------------
//.......
//你的上面的代码
convertView = textView;
else {
if (convertView instanceof TextView) {
((TextView) convertView).setText(group[position]);
}
}
return convertView;
@Override还是报一样的错啊 --------------------编程问答-------------------- 你试试改成这样:
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
//TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout =(LinearLayout)inflater.inflate(R.layout.group,null);
TextView textView=(TextView)layout.findViewById(R.id.group);
textView.setText(group[groupPosition]);
if(convertView==null){
convertView=textView;
}else
{
if(convertView instanceof TextView){
((TextView)convertView).setText(group[groupPosition]);
}
}
return convertView;
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
//TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView =inflater.inflate(R.layout.group,null);
TextView textView=(TextView)layout.findViewById(R.id.group);
textView.setText(group[groupPosition]);
if(convertView==null){
convertView=textView;
}else
{
if(convertView instanceof TextView){
((TextView)convertView).setText(group[groupPosition]);
}
}
return convertView;
注意看报错信息 --------------------编程问答-------------------- 第一次贴代码,不熟悉,就是改这一句:
convertView =inflater.inflate(R.layout.group,null); --------------------编程问答--------------------
这么改是对的,我所不明白的是返回一个View对象对,那我返回一个TextView对象为什么不对呢?想子对象赋给父类难道有错吗 --------------------编程问答-------------------- 我觉得问题是你没有对convertView初始化,这次你试试改返回Textview对象试试,看他有没有报错咯,我觉得是getView方法自动调用convertView来填充,所以要返回convertView对象呀 --------------------编程问答-------------------- 你返回的view应该会被addView到父view上面,而你的textview已经有父view,我觉得是报iew has parent...的错
补充:移动开发 , Android