MisterM2402
MisterM2402

Reputation: 127

NullPointerException when using a ListView

I'm struggling to find the cause of a certain NullPointerException that's being thrown when I try to start a specific activity.

LogCat output:

12-11 13:29:28.211: E/AndroidRuntime(701): Caused by: java.lang.NullPointerException
12-11 13:29:28.211: E/AndroidRuntime(701):  at android.content.res.AssetManager.getResourceTextArray(AssetManager.java:214)
12-11 13:29:28.211: E/AndroidRuntime(701):  at android.content.res.Resources.getTextArray(Resources.java:361)
12-11 13:29:28.211: E/AndroidRuntime(701):  at android.content.res.TypedArray.getTextArray(TypedArray.java:628)
12-11 13:29:28.211: E/AndroidRuntime(701):  at android.widget.ListView.<init>(ListView.java:168)
12-11 13:29:28.211: E/AndroidRuntime(701):  at android.widget.ListView.<init>(ListView.java:159)
12-11 13:29:28.211: E/AndroidRuntime(701):  ... 25 more

onCreate() method of the activity in question:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display);
}

XML for the layout "display" that my activity is trying to load:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false" >

    <ListView
        android:id="@+id/weekdayListView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

    </ListView>

    <ListView
        android:id="@+id/timeSlotListView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:entries="@layout/item_layout" >

    </ListView>

</LinearLayout>

Other than what's in onCreate(), my activity doesn't do anything else. Since there's no code to populate the listviews (there was but I commented it out in trying to find the root of the problem), when the activity starts it should just be a blank screen.

When I comment out the section in the XML file relating to weekdayListView, the activity loads just fine, so I have to assume there's a problem with how I've set up the XML. The only difference I can see between the two is that I don't explicitly set the android:entries attribute, but if I understand it correctly, in this case it should just use the default.

I've been stuck on this for a while now so any help is greatly appreciated. I tried to provide as much information as I thought was relevant but if there's anything else I forgot to include, let me know.

Upvotes: 0

Views: 260

Answers (1)

Michael
Michael

Reputation: 1496

Your android:entries attribute points to a layout when it should rather point to a string array resource.

Create a string array in your strings.xml, e.g.

<string-array name="vals">
  <item>Foo</item>
  <item>Bar</item>
</string-array>

and then use

android:entries="@string/vals"

Upvotes: 3

Related Questions