java - CustomAdapter 类中的 ClassCastException

标签 java android android-arrayadapter classcastexception android-radiobutton

所以我在特定行上得到了这个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;
    }
}

mySelectedRadioButton = (RadioButton) v 的 onClick() 方法中的 setOnClickListener() 中抛出Exception ;//ClassCastException 在这一行 行中。

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)

现在我不知道为什么抛出异常。任何帮助将不胜感激。谢谢。

最佳答案

是的,因为您要将监听器添加到 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;
       ...

    }
});    

关于java - CustomAdapter 类中的 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31752193/

相关文章:

java - 在动画期间更改 Z 顺序

java - 检查从 arrayadapter 获取的复选框

android - 自定义 ArrayList 适配器 - 在 ActionBar 列表中显示图标

android - 自定义数组适配器初始化错误

java - 我想在 Ubuntu 12.04LTS 中制作直接执行 jar 文件的 sh 文件

java - Android Layout_Weight 在 TableLayout 中无法正常工作?

java - Hbase 示例,线程 "main"java.lang.NoClassDefFoundError 中的异常

java - 如何在 MigLayout 中的垂直单元内堆叠组件?

java - view = inflater.inflate(R.layout.video_list_item,parent,false);导致强制关闭错误

android - 有没有办法通过 Google API 获取用户的 "home"和 "work"位置?