我正在使用BaseAdapter
类,当我尝试投射viewHolder类时,ClassCastException
方法出现getView
错误。
这是我的代码:
public View getView(int position, View convertView, ViewGroup parent){
// TODO Auto-generated method stub
NoteItem noteItem = list.get(position);
TextView tv;
LinearLayout ll;
if(convertView == null){
convertView = inflater.inflate(R.layout.dialog_listitem_note, null);
ll = (LinearLayout) convertView.findViewById(R.id.llNote);
tv = (TextView) convertView.findViewById(R.id.tvNoteItem);
convertView.setTag(new NoteItemViewHolder(tv, ll));
ll.setOnClickListener(new OnClickListener(){...});
}else{
NoteItemViewHolder viewHolder = (NoteItemViewHolder) convertView.getTag(); //ClassCastException error here
ll = viewHolder.getLl();
tv = viewHolder.getName();
}
tv.setText(noteItem.getName());
ll.setTag(noteItem);
...
return convertView;
}
private class NoteItemViewHolder{
TextView name;
LinearLayout ll;
public NoteItemViewHolder(TextView name, LinearLayout ll){
this.name = name;
this.ll = ll;
}
public TextView getName(){
return name;
}
public LinearLayout getLl(){
return ll;
}
}
logcat的:
04-16 14:10:04.979: E/AndroidRuntime(23879): FATAL EXCEPTION: main
04-16 14:10:04.979: E/AndroidRuntime(23879): java.lang.ClassCastException: com.calllogimprovementtools.ainius.CallLogView$NoteItem
04-16 14:10:04.979: E/AndroidRuntime(23879): at com.calllogimprovementtools.ainius.CallLogView$MyNoteAdapter.getView(CallLogView.java:300)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.widget.AbsListView.obtainView(AbsListView.java:1409)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.widget.ListView.makeAndAddView(ListView.java:1745)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.widget.ListView.fillDown(ListView.java:670)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.widget.ListView.fillGap(ListView.java:641)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:3407)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.widget.AbsListView.onTouchEvent(AbsListView.java:2237)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.widget.ListView.onTouchEvent(ListView.java:3446)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.view.View.dispatchTouchEvent(View.java:3885)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-16 14:10:04.979: E/AndroidRuntime(23879): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1703)
04-16 14:10:04.979: E/AndroidRuntime(23879): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1133)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.app.Dialog.dispatchTouchEvent(Dialog.java:642)
04-16 14:10:04.979: E/AndroidRuntime(23879): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1687)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2196)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.view.ViewRoot.handleMessage(ViewRoot.java:1880)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.os.Looper.loop(Looper.java:123)
04-16 14:10:04.979: E/AndroidRuntime(23879): at android.app.ActivityThread.main(ActivityThread.java:3729)
04-16 14:10:04.979: E/AndroidRuntime(23879): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 14:10:04.979: E/AndroidRuntime(23879): at java.lang.reflect.Method.invoke(Method.java:507)
04-16 14:10:04.979: E/AndroidRuntime(23879): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
04-16 14:10:04.979: E/AndroidRuntime(23879): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
04-16 14:10:04.979: E/AndroidRuntime(23879): at dalvik.system.NativeStart.main(Native Method)
不知道为什么会出现这种错误,请帮助。
答案 0 :(得分:0)
getView 方法应如下所示,并检查您的控件,如LinearLayout与R.id.yourLinearID绑定
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(view == null)
{
view = inflater.inflate(R.layout.checkin_item, null);
holder = new ViewHolder();
holder.iconImage = (ImageView)view.findViewById(R.id.chkin_item_iv);
holder.placeName = (TextView)view.findViewById(R.id.chkin_item_tv_name);
view.setTag(holder);
}
else
{
holder = (ViewHolder)view.getTag();
}
// here need to Bind Require Control
holder.placeName.setText(googleSetGetArray.getGoogleSetGets().get(position).getName());
return view;
}
class ViewHolder
{
ImageView iconImage;
TextView placeName;
}
答案 1 :(得分:0)
像这样使用:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder2;
View hView = convertView;
if (convertView == null) {
holder2 = new ViewHolder();
hView.setTag(holder2);
} else {
holder2 = (ViewHolder) hView.getTag();
}
}
答案 2 :(得分:0)
我解决了错误。问题是我设置了不同的标签,将convertView ant转换为ll(linearlayout),这是相同的视图,所以当convertView!=null
getTag()
返回NoteItem
项时。