java.lang.ClassCastException,尝试让两个活动使用同一个自定义适配器

java.lang.ClassCastException, trying to have two activites using the same custom adapter

我有两个活动,NewContact.javaViewContact.java,我想使用相同的 custom adapter,因为这两个活动非常相似。

但是我收到以下错误并且我的应用程序崩溃了:

AndroidRuntime: FATAL EXCEPTION: main
java.lang.ClassCastException: com.example.chris.tutorialspoint.ViewContact cannot be cast to com.example.chris.tutorialspoint.NewContact

我在这里阅读了很多主题为 AndroidRuntime: FATAL EXCEPTION: 的帖子,java.lang.ClassCastException但我不知道如何调整答案以满足我的需要。

这是我的适配器的 getView 代码。我知道问题出在以下几行:

    viewHolder.check.setOnCheckedChangeListener((NewContact) _c);
    viewHolder.check.setOnCheckedChangeListener((ViewContact) _c);

如果我删除其中一行,那么 custom adapter 对剩余的 activity 起作用,但我想让它对两个活动起作用,NewContactViewContact .

 @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        System.out.println("getView number is :" + i + "convertView is : " + convertView);
        //we're naming our convertView as view
        //  View view = convertView;
        ViewHolder viewHolder = null;

        if (convertView == null) {

            //if there is nothing there (if it's null) inflate the view with the layout
            LayoutInflater li = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = li.inflate(R.layout.phone_inflate_listview, null);

            viewHolder = new ViewHolder();
            //      So, for example, title is cast to the name id, in phone_inflate_listview,
            //      phone is cast to the id called no etc
            viewHolder.title = (TextView) convertView.findViewById(R.id.name);
            viewHolder.phone = (TextView) convertView.findViewById(R.id.no);
            viewHolder.invite = (Button)  convertView.findViewById(R.id.btnInvite);
            viewHolder.check = (CheckBox) convertView.findViewById(R.id.checkBoxContact);
            // viewHolder.check.setVisibility(View.GONE);

            //remember the state of the checkbox
            viewHolder.check.setOnCheckedChangeListener((NewContact) _c);
            viewHolder.check.setOnCheckedChangeListener((ViewContact) _c);

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }
//        store the holder with the view
        final SelectPhoneContact data = (SelectPhoneContact) arraylist.get(i);
        //in the listview for contacts, set the name
        viewHolder.title.setText(data.getName());
        //in the listview for contacts, set the number
        viewHolder.phone.setText(data.getPhone());

        ////*********************

        //for every phone number in the MatchingContactsAsArrayList array list...
        for (int number = 0; number < MatchingContactsAsArrayList.size(); number++) {

            //if a phone number is in our array of matching contacts
            if (MatchingContactsAsArrayList.contains(data.getPhone()))

            {
                //if a matching contact, no need to show the Invite button
                viewHolder.invite.setVisibility(View.GONE);
                System.out.println("it's a match: phoneNumberofContact is : " + data.getPhone());
                //once a matching contact is found, no need to keep looping x number of time, move onto next contact
                break;

            }

            else {
                //if not a matching contact, no need to show the check box
                viewHolder.check.setVisibility(View.GONE);

            }

        }


        viewHolder.check.setChecked(data.isSelected());



        viewHolder.check.setTag(data);

        // Return the completed view to render on screen

        return convertView;

    }

您不需要将 Context 转换为特定的 Activity 类型。 setOnCheckedChangeListener() 方法只需要一个 OnCheckedChangeListener,如果两个 类 都实现了该接口,则您只需要一个将 Context 转换为 OnCheckedChangeListener 的调用。

viewHolder.check.setOnCheckedChangeListener((OnCheckedChangeListener) _c);

在构造函数中添加一个 instanceof 检查以确保传递的 Context 确实是一个 OnCheckedChangeListener 可能是明智的,这将有更快失败的好处,并且让您有机会抛出一个 Exception 并提供更多信息。