我有一个从 web 服务获取 HTTP 响应的 android 应用程序,我将其处理成字符串数组。我尝试在 ListView 中显示这些字符串。它们不需要被点击来执行任何操作,只需显示并且可以滚动。
运行时,一切正常,但是当我尝试向下滚动超出我必须显示的项目数量时(因为列表上没有要显示的字符串),我得到一个 NullPointerException。显然我一定做错了什么。
我不太明白。我写了一个类似的例子作为测试,它工作得很好。示例中的列表仅在没有更多项目可显示时滚动并停止。我能想到的唯一区别是,在原始应用程序上,数组是在线程上创建的,然后线程使用处理程序运行可运行文件以在 gui 上显示列表。
当获取字符串并将它们命令到数组中显示的线程完成时,他使用处理程序运行此代码:
scoreList = (ListView) findViewById(R.id.scorelist);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,scoreResult);
scoreList.setAdapter(myAdapter);
比尝试滚动低于底部时应用程序崩溃。这是来自 adb logcat 的错误:
D/AndroidRuntime( 864): Shutting down VM
W/dalvikvm( 864): threadid=1: thread exiting with uncaught exception (group=0x4
001d800)
E/AndroidRuntime( 864): FATAL EXCEPTION: main
E/AndroidRuntime( 864): java.lang.NullPointerException
E/AndroidRuntime( 864): at android.widget.ArrayAdapter.createViewFromRes
ource(ArrayAdapter.java:355)
E/AndroidRuntime( 864): at android.widget.ArrayAdapter.getView(ArrayAdap
ter.java:323)
E/AndroidRuntime( 864): at android.widget.AbsListView.obtainView(AbsList
View.java:1294)
E/AndroidRuntime( 864): at android.widget.ListView.makeAndAddView(ListVi
ew.java:1727)
E/AndroidRuntime( 864): at android.widget.ListView.fillDown(ListView.jav
a:652)
E/AndroidRuntime( 864): at android.widget.ListView.fillGap(ListView.java
:623)
E/AndroidRuntime( 864): at android.widget.AbsListView.trackMotionScroll(
AbsListView.java:2944)
E/AndroidRuntime( 864): at android.widget.AbsListView.onTouchEvent(AbsLi
stView.java:2065)
E/AndroidRuntime( 864): at android.widget.ListView.onTouchEvent(ListView
.java:3315)
E/AndroidRuntime( 864): at android.view.View.dispatchTouchEvent(View.jav
a:3766)
E/AndroidRuntime( 864): at android.view.ViewGroup.dispatchTouchEvent(Vie
wGroup.java:897)
E/AndroidRuntime( 864): at android.view.ViewGroup.dispatchTouchEvent(Vie
wGroup.java:936)
E/AndroidRuntime( 864): at android.view.ViewGroup.dispatchTouchEvent(Vie
wGroup.java:936)
E/AndroidRuntime( 864): at android.view.ViewGroup.dispatchTouchEvent(Vie
wGroup.java:936)
E/AndroidRuntime( 864): at com.android.internal.policy.impl.PhoneWindow$
DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
E/AndroidRuntime( 864): at com.android.internal.policy.impl.PhoneWindow.
superDispatchTouchEvent(PhoneWindow.java:1107)
E/AndroidRuntime( 864): at android.app.Activity.dispatchTouchEvent(Activ
ity.java:2086)
E/AndroidRuntime( 864): at com.android.internal.policy.impl.PhoneWindow$
DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
E/AndroidRuntime( 864): at android.view.ViewRoot.handleMessage(ViewRoot.
java:1785)
E/AndroidRuntime( 864): at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime( 864): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 864): at android.app.ActivityThread.main(ActivityThrea
d.java:4627)
E/AndroidRuntime( 864): at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime( 864): at java.lang.reflect.Method.invoke(Method.java:5
21)
E/AndroidRuntime( 864): at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 864): at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:626)
E/AndroidRuntime( 864): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 59): Force finishing activity memo.ori.friend/.Highscores
W/ActivityManager( 59): Activity pause timeout for HistoryRecord{4503fed0 memo
.ori.friend/.Highscores}
W/WindowManager( 59): No window to dispatch pointer action 1
W/ActivityManager( 59): Launch timeout has expired, giving up wake lock!
W/ActivityManager( 59): Activity idle timeout for HistoryRecord{45023af0 memo.
ori.friend/.MemoFriendActivity}
D/dalvikvm( 261): GC_EXPLICIT freed 45 objects / 2128 bytes in 108ms
W/ActivityManager( 59): Activity destroy timeout for HistoryRecord{4503fed0 me
mo.ori.friend/.Highscores}
D/SntpClient( 59): request time failed: java.net.SocketException: Address fami
ly not supported by protocol
I/Process ( 864): Sending signal. PID: 864 SIG: 9
I/ActivityManager( 59): Process memo.ori.friend (pid 864) has died.
I/WindowManager( 59): WIN DEATH: Window{45077ce0 memo.ori.friend/memo.ori.frie
nd.MemoFriendActivity paused=false}
I/WindowManager( 59): WIN DEATH: Window{4507cf60 memo.ori.friend/memo.ori.frie
nd.Highscores paused=false}
I/UsageStats( 59): Unexpected resume of com.android.launcher while already res
umed in memo.ori.friend
W/InputManagerService( 59): Got RemoteException sending setActive(false) notif
ication to pid 864 uid 10044
W/IInputConnectionWrapper( 126): showStatusIcon on inactive InputConnection
D/SntpClient( 59): request time failed: java.net.SocketException: Address fami
ly not supported by protocol