0

我正在使用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)

不明白为什么会发生此错误,请帮助。

4

3 回答 3

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;         
    }
于 2013-04-16T10:24:27.337 回答
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();
    }
}
于 2013-04-16T10:33:23.480 回答
0

我解决了错误。问题是我将不同的标签设置为 convertView ant to ll (linearlayout) 这是相同的视图,所以当convertView!=null返回的getTag()项目NoteItem时。

于 2013-04-18T13:51:12.637 回答