ClassCastException в классе CustomAdapter


Таким образом, я получаю это ClassCastException на конкретной строке:

Java :

private class MyArrayAdapter extends ArrayAdapter<MyItem> // My custom array adapter class
{
    private int myResourceId = 0;
    private LayoutInflater myLayoutInflater; 
    private RadioButton mySelectedRadioButton;
    private int mSelectedPosition = -1;
    private ButtonClickListener myClickListener = null;

    public MyArrayAdapter(Context context, int myResourceId, List<MyItem> objects,ButtonClickListener myClickListener) 
    {
        super(context, myResourceId, myItemList);
        this.myResourceId = myResourceId;
        myLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.myClickListener = myClickListener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        final ViewHolder holder;

        if (view == null)
        {

            view = myLayoutInflater.inflate(myResourceId, parent, false);
            holder = new ViewHolder();

            holder.dateTextView = (TextView) view.findViewById(R.id.dates_id);
            holder.addDateButton = (Button) view.findViewById(R.id.add_date_button_id);
            holder.addCommentButton = (Button)view.findViewById(R.id.add_comment_button_id);
            holder.selectDateRadioButton = (RadioButton) view.findViewById(R.id.select_date_radio_button_id);

            holder.addDateButton.setOnClickListener(new View.OnClickListener()
            {   
                @Override
                public void onClick(View v) 
                {
                    holder.dateTextView.setText(finalDateShown);
                }
            });

            view.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) view.getTag();
        }

        holder.addDateButton.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                if(position != mSelectedPosition && mySelectedRadioButton != null)
                {
                    mySelectedRadioButton.setChecked(false);
                }
                mSelectedPosition = position;
                mySelectedRadioButton = (RadioButton) v; // ClassCastException is in this line
                Log.d("debug", finalDateShown);
                add(new MyItem(finalDateShown));
            }
        });


        if(mSelectedPosition != position)
        {
            holder.selectDateRadioButton.setChecked(false);
        }
        else
        {
            holder.selectDateRadioButton.setChecked(true);
            if(mySelectedRadioButton != null && holder.selectDateRadioButton != mySelectedRadioButton)
            {
                mySelectedRadioButton = holder.selectDateRadioButton;
            }
        }
        return view;
    } // End of getView() method



    @Override
    public void add(MyItem object) 
    {
        super.add(object);

    }



    private class ViewHolder
    {
        TextView dateTextView;
        Button addDateButton;
        Button addCommentButton;
        RadioButton selectDateRadioButton;
    }
}

Exception выбрасывается в setOnClickListener() в методе onClick() в строке mySelectedRadioButton = (RadioButton) v; // ClassCastException is in this line.

LogCat :

07-31 20:42:40.354: E/AndroidRuntime(11405): FATAL EXCEPTION: main
07-31 20:42:40.354: E/AndroidRuntime(11405): Process:    com.example.mylifeinformationsharedsocial, PID: 11405
07-31 20:42:40.354: E/AndroidRuntime(11405): java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.RadioButton
07-31 20:42:40.354: E/AndroidRuntime(11405):    at com.example.mylifeinformationsharedsocial.SexAcivity$MyArrayAdapter$2.onClick(SexAcivity.java:314)
07-31 20:42:40.354: E/AndroidRuntime(11405):    at android.view.View.performClick(View.java:4763)
07-31 20:42:40.354: E/AndroidRuntime(11405):    at android.view.View$PerformClick.run(View.java:19821)
07-31 20:42:40.354: E/AndroidRuntime(11405):    at android.os.Handler.handleCallback(Handler.java:739)
07-31 20:42:40.354: E/AndroidRuntime(11405):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-31 20:42:40.354: E/AndroidRuntime(11405):    at android.os.Looper.loop(Looper.java:135)
07-31 20:42:40.354: E/AndroidRuntime(11405):    at android.app.ActivityThread.main(ActivityThread.java:5274)
07-31 20:42:40.354: E/AndroidRuntime(11405):    at java.lang.reflect.Method.invoke(Native Method)
07-31 20:42:40.354: E/AndroidRuntime(11405):    at java.lang.reflect.Method.invoke(Method.java:372)
07-31 20:42:40.354: E/AndroidRuntime(11405):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
07-31 20:42:40.354: E/AndroidRuntime(11405):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

Теперь я понятия не имею, почему возникает исключение. Любая помощь, которая будет очень признательна. Спасибо.

1 2

1 ответ:

Да, потому что вы добавляете слушателя к holder.addDateButton, который является Button.

Вы можете добавить слушателя в holder.selectDateRadioButton.

Просто измените:

holder.addDateButton.setOnClickListener(new View.OnClickListener() 
{
   @Override
   public void onClick(View v) 
   {
      ...
      mySelectedRadioButton = (RadioButton) v;
      ...

   }
});

Кому:

holder.selectDateRadioButton.setOnClickListener(new View.OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
       ...
       mySelectedRadioButton = (RadioButton) v;
       ...

    }
});