0

I have the following GridView and it crashes at times giving me a NullPointer exception in the Adroid GridView.java source code.

It seems that this only happens on Android 2.3.7 - Gingerbread API 10.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_gridFragment"

        >

    <include
        layout="@layout/toolbar_new"/>


    <include
        layout="@layout/heading_bar"
        />


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <GridView
            android:id="@+id/main_grid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnWidth="100dp"
            android:numColumns="auto_fit"
            android:verticalSpacing="0dp"
            android:horizontalSpacing="10dp"
            android:stretchMode="columnWidth"
            android:gravity="center"
            >
        </GridView>

        <!-- the Z index is determined by ordering, this is displayed over the Gridview -->
        <!-- android:id="@+id/menu" -->
        <include
            layout="@layout/menu"/>

    </FrameLayout>


</LinearLayout>

The Stack trace is here:

FATAL EXCEPTION: main
java.lang.NullPointerException
    at android.widget.GridView.fillUp(GridView.java:311)
    at android.widget.GridView.fillGap(GridView.java:199)
    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:3422)
    at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2920)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3703)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.7_r1/android/widget/GridView.java#GridView.0mReferenceView

It seems that mReferenceView is null at line 311. //mReferenceView will change with each call to makeRow()

Wayne
  • 3,359
  • 3
  • 30
  • 50
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – NoChinDeluxe Jan 20 '16 at 15:37

1 Answers1

0

I have successfully found the issue that caused the problem. Gingerbread fails and crashed as per the stack trace above when calling:

gridView.smoothScrollToPosition(0);

By simple removing the call I managed to fix the problem. Tested this on actual device:

Motorola MB525, Android version 2.3.7
More details on the device:
Kernel 2.6.32.9-gb08c3c8 Build number MUI-3.1.27, Fri Jun 3 11:40:33 CST 2011

This does not FIX THE BUG:

            gridView.post(new Runnable() {
                @Override
                public void run() {
                    gridView.smoothScrollToPosition(0);
                }
            });
Wayne
  • 3,359
  • 3
  • 30
  • 50