Sudarshan
Sudarshan

Reputation: 18534

Null Pointer Exception on setAdapter Action for Expandable List View

I try to run my application, but i encounter null pointer exception at setAdapter(listAdapter) action.

Main Activity Java code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Assigning main activity name for drawing
    setContentView(R.layout.activity_time);
    // Find the ListView resource and assign to ListView Object created
    ExpandableListView mainListView = (ExpandableListView) findViewById(R.id.textClock);
    // Initialize adapter with data by parsing JSON
    listAdapter = new TwocolumnAdapter(this, parseJSONString(readFile()), headersList);
    //Saving the reference to prepare new adapter when reload is needed
    object = this;
    // Set the ArrayAdapter as the ListView's adapter.
    mainListView.setAdapter(listAdapter);
}

Main Activity XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".Time" android:background="@color/accent_material_dark" android:orientation="vertical">
<ExpandableListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textClock" android:layout_gravity="center_vertical" android:format24Hour="@android:string/yes" />

I searched several SO posts, cross checked id(s) etc, tried all solutions but could not nail this issue.

Any suggestions or pointers are highly appreciated.

Please check complete code on GITHUB

Error Logs

12-18 00:36:59.609  19893-19893/sudharshanapps.clock E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: sudharshanapps.clock, PID: 19893
    java.lang.RuntimeException: Unable to start activity ComponentInfo{sudharshanapps.clock/sudharshanapps.clock.Time}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at sudharshanapps.clock.Time.onCreate(Time.java:264)
            at android.app.Activity.performCreate(Activity.java:5248)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 663

Answers (1)

Dave S
Dave S

Reputation: 3468

It looks like the problem is activity_time.xml in the Layout-v17 folder. This is the layout that will be used for API 17 and higher. It looks like in this case it's actually loading this xml instead of the one you expect. Try deleting it or making it the same as the main version of activity_time.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mainListView" >
    </ListView>
</LinearLayout>

Upvotes: 1

Related Questions