CustomAdapter 中的 ClassCastException class
ClassCastException in a CustomAdapter class
所以我在特定的行上得到这个 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; // ClassCastException is in this line
行的onClick()
方法中的setOnClickListener()
抛出了Exception
。
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.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;
...
}
});
所以我在特定的行上得到这个 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; // ClassCastException is in this line
行的onClick()
方法中的setOnClickListener()
抛出了Exception
。
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.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;
...
}
});