我正在運行ListView
的以下代碼onItemClickListener
。但它表明ClassCastException
當我把串在位置ARG2在字符串s爲:從列表視圖中獲取字符串時拋出類異常
String s= (String) arg0.getItemAtPosition(arg2);
代碼片段:
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
String out;
String s = (String) arg0.getItemAtPosition(arg2);
//some code
}
}
我張貼的堆棧跟蹤也:
07-14 02:35:42.798: E/AndroidRuntime(412): FATAL EXCEPTION: main
07-14 02:35:42.798: E/AndroidRuntime(412): java.lang.ClassCastException: bitcream.whats.your.score.Rest
07-14 02:35:42.798: E/AndroidRuntime(412): at bitcream.whats.your.score.Intermediate$1.onItemClick(Intermediate.java:55)
07-14 02:35:42.798: E/AndroidRuntime(412): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-14 02:35:42.798: E/AndroidRuntime(412): at android.widget.ListView.performItemClick(ListView.java:3513)
07-14 02:35:42.798: E/AndroidRuntime(412): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
07-14 02:35:42.798: E/AndroidRuntime(412): at android.os.Handler.handleCallback(Handler.java:587)
07-14 02:35:42.798: E/AndroidRuntime(412): at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 02:35:42.798: E/AndroidRuntime(412): at android.os.Looper.loop(Looper.java:123)
07-14 02:35:42.798: E/AndroidRuntime(412): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-14 02:35:42.798: E/AndroidRuntime(412): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 02:35:42.798: E/AndroidRuntime(412): at java.lang.reflect.Method.invoke(Method.java:507)
07-14 02:35:42.798: E/AndroidRuntime(412): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-14 02:35:42.798: E/AndroidRuntime(412): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-14 02:35:42.798: E/AndroidRuntime(412): at dalvik.system.NativeStart.main(Native Method)
07-14 02:35:45.468: I/Process(412): Sending signal. PID: 412 SIG: 9
請提出修復建議。
適配器代碼:
public class Myadapter extends ArrayAdapter<Rest>{
Context ctx;
List<Rest> list;
Rest r1;
ImageView iv1;
TextView tv1, tv2;
public Myadapter(Context context, int textViewResourceId,
List<Rest> objects) {
super(context, textViewResourceId, objects);
ctx = context;
list = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater lii = (LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
View row = lii.inflate(R.layout.li, null, false);
tv1 = (TextView) row.findViewById(R.id.textView1);
r1 = list.get(position);
tv1.setText(r1.name);
work();
return row;
}
其餘類是:
package bitcream.whats.your.score;
public class Rest {
String name;
public Rest(String name) {
this.name = name;
}
}
是否聲明arg0.getItemAtPosition(ARG2)返回字符串?如果在將它們存儲到字符串之前沒有先將它們轉換爲字符串。 – CENT1PEDE
發佈適配器代碼 – Raghunandan
發佈其餘類代碼。你有吸氣和吸氣的嗎?也張貼適配器代碼,看看http://developer.android.com/reference/android/widget/AdapterView.html#getItemAtPosition(int) – Raghunandan