所以我在特定的行上得到ClassCastException
:
爪哇:
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)
现在我不知道为什么会抛出异常。任何帮助都应该非常感激。感谢。
答案 0 :(得分:2)
是的,因为您要将监听器添加到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;
...
}
});