我遵循了这个 RSS 阅读器教程。
所以起初我在这一行遇到了一个错误:
Uri uri = Uri.parse(links.get(position));
我得到了错误,我必须将它转换为这样的字符串:
Uri uri = Uri.parse((String) links.get(position));
之后就可以运行应用程序了。但是,如果我想打开一个 ListView 元素,应用程序每次都会停止......有没有人知道出了什么问题?
这里是logcat:
11-20 19:25:19.027: E/AndroidRuntime(505): FATAL EXCEPTION: main
11-20 19:25:19.027: E/AndroidRuntime(505): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 0
11-20 19:25:19.027: E/AndroidRuntime(505): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
11-20 19:25:19.027: E/AndroidRuntime(505): at java.util.ArrayList.get(ArrayList.java:311)
11-20 19:25:19.027: E/AndroidRuntime(505): at com.rss_reader.MainActivity.onListItemClick(MainActivity.java:111)
11-20 19:25:19.027: E/AndroidRuntime(505): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
11-20 19:25:19.027: E/AndroidRuntime(505): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-20 19:25:19.027: E/AndroidRuntime(505): at android.widget.ListView.performItemClick(ListView.java:3513)
11-20 19:25:19.027: E/AndroidRuntime(505): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
11-20 19:25:19.027: E/AndroidRuntime(505): at android.os.Handler.handleCallback(Handler.java:587)
11-20 19:25:19.027: E/AndroidRuntime(505): at android.os.Handler.dispatchMessage(Handler.java:92)
11-20 19:25:19.027: E/AndroidRuntime(505): at android.os.Looper.loop(Looper.java:123)
11-20 19:25:19.027: E/AndroidRuntime(505): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-20 19:25:19.027: E/AndroidRuntime(505): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 19:25:19.027: E/AndroidRuntime(505): at java.lang.reflect.Method.invoke(Method.java:507)
11-20 19:25:19.027: E/AndroidRuntime(505): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-20 19:25:19.027: E/AndroidRuntime(505): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-20 19:25:19.027: E/AndroidRuntime(505): at dalvik.system.NativeStart.main(Native Method)
在我阅读描述后,我认为 Uri 可能为空,因为:
如果 uriString 为空,则抛出 NullPointerException
但为什么它为空?我不知道...
编辑:教程的链接是错误的......但现在我找到了正确的。