0

単語検索をスクロールしようとすると、プログラムがクラッシュしてヌル ポインター例外がスローされます。ただし、理由はわかりませんが、

私のグリッドビューは配列で作成されています

    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)

何か案は?

4

2 に答える 2

3

for ループが 1 から始まり、そのようにデータを挿入するのはなぜですか? これは、for ループが文字列を配列NullPointerExceptionに挿入することが原因である可能性が最も高いです。WordSearch

例えば。

FormattedWords が Array (.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 の最初の値を省略する場合は、NPE を回避するためWordSearchにサイズ設定を開始する必要があります。FormattedWords.lenght - 1

例えば。

String [] WordSearch =  new String[FormattedWords.length - 1];
于 2013-04-17T14:53:41.800 に答える
0

の値FormattedWords[i]が等しいnull場合、ビューが前面に表示されたときにアプリがクラッシュすることを意味します

于 2015-12-14T07:15:56.280 に答える