当我尝试滚动浏览单词时,程序崩溃并抛出空指针异常。
但是我不知道为什么
我的网格视图是用数组创建的
String [] WordSearch = new String[FormattedWords.length];
for(int i = 1; i<FormattedWords.length ;i++)
{
WordSearch[i-1] = FormattedWords[i];
}
,logcat也给了我
04-17 14:19:37.623: E/AndroidRuntime(5969): FATAL EXCEPTION: main
04-17 14:19:37.623: E/AndroidRuntime(5969): java.lang.NullPointerException
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.widget.AbsListView.obtainView(AbsListView.java:2267)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.widget.GridView.makeAndAddView(GridView.java:1331)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.widget.GridView.makeRow(GridView.java:331)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.widget.GridView.fillDown(GridView.java:283)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.widget.GridView.fillGap(GridView.java:243)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5036)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3193)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.widget.AbsListView.startScrollIfNeeded(AbsListView.java:3138)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3463)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.View.dispatchTouchEvent(View.java:7127)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
04-17 14:19:37.623: E/AndroidRuntime(5969): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
04-17 14:19:37.623: E/AndroidRuntime(5969): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
04-17 14:19:37.623: E/AndroidRuntime(5969): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.View.dispatchPointerEvent(View.java:7307)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4203)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4243)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.Choreographer.doFrame(Choreographer.java:523)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.os.Handler.handleCallback(Handler.java:615)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.os.Looper.loop(Looper.java:137)
04-17 14:19:37.623: E/AndroidRuntime(5969): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-17 14:19:37.623: E/AndroidRuntime(5969): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 14:19:37.623: E/AndroidRuntime(5969): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 14:19:37.623: E/AndroidRuntime(5969): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-17 14:19:37.623: E/AndroidRuntime(5969): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-17 14:19:37.623: E/AndroidRuntime(5969): at dalvik.system.NativeStart.main(Native Method)
有任何想法吗?
最佳答案
为什么for循环从1开始并插入数据呢? NullPointerException
很可能是由for循环导致将字符串插入WordSearch
数组引起的。
例如。
假设FormattedWords是一个数组(您正在使用.length属性),如果FormattedWords.length
为3并包含:
FormattedWords[0] = "a";
FormattedWords[1] = "b";
FormattedWords[2] = "c";
在for循环之后,
WordSearch
将包含:WordSearch[0] = "b";
WordSearch[1] = "c";
WordSearch[2] = null;
您的问题似乎在这里。
编辑
如果要忽略FormattedWords中的第一个值,则应将
WordSearch
设置为大小FormattedWords.lenght - 1
,以避免NPE。例如。
String [] WordSearch = new String[FormattedWords.length - 1];