我正在使用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;
}
}
日志猫:
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)
不明白为什么会发生此错误,请帮助。