当我尝试滚动单词搜索时,我的程序崩溃抛出空指针异常。 但是我不明白为什么,
我的网格视图是使用数组
创建的 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)
有什么想法吗?
答案 0 :(得分:3)
为什么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];
答案 1 :(得分:0)
如果FormattedWords[i]
的值等于null
,则表示视图出现时app会崩溃