单击列表视图时出现错误,我已粘贴代码

时间:2011-12-01 05:03:54

标签: android

在onclick事件中,我希望在Integer中选择列表的位置,但是我收到错误。

这是我在onclick事件中编写的代码

Integer item = (Integer) getListAdapter().getItem(position);

这是我得到的错误,所以任何人都可以帮助我解决错误。

我有一个类调用Searcpage wch由简单的listview

组成
12-01 04:59:07.759: ERROR/AndroidRuntime(405): FATAL EXCEPTION: main
12-01 04:59:07.759: ERROR/AndroidRuntime(405): java.lang.ClassCastException: java.lang.String
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at     com.search.SearchPage.onListItemClick(SearchPage.java:73)
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at android.app.ListActivity$2.onItemClick(ListActivity.java:321)
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at android.widget.ListView.performItemClick(ListView.java:3382)
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at android.os.Handler.handleCallback(Handler.java:587)
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at android.os.Looper.loop(Looper.java:123)
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at java.lang.reflect.Method.invokeNative(Native Method)
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at java.lang.reflect.Method.invoke(Method.java:521)
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-01 04:59:07.759: ERROR/AndroidRuntime(405):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

try {
    int item = Integer.parse(getListAdapter().getItem(position));
} catch (Exception ex) {
    // error log: ex.getMessage()
}