0

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.

MisterM2402
  • 127
  • 5
  • Can you provide the rest of the code of your activity please. Does it extend listactivity ? – HpTerm Dec 11 '13 at 14:18
  • When does it work fine?When you comment `weekdayListView` or `timeSlotListView`? – Apoorv Dec 11 '13 at 14:18
  • @HpTerm: Besides the onCreate() method listed above, there's only the surrounding `public class Display extends Activity {}`, the package name and some currently unused imports. @Apoorv: It works fine when I comment out `weekdayListView`, and by that I mean the blank `timeSlotListView` is displayed when I start the activity. – MisterM2402 Dec 11 '13 at 14:52
  • My apologies, I find on testing it again it's actually `weekdayListView` that works when I comment out `timeSlotListView`. – MisterM2402 Dec 11 '13 at 15:01
  • 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) – Albireo Mar 07 '16 at 08:55

1 Answers1

3

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"
Michael
  • 1,416
  • 1
  • 9
  • 21
  • It points to a layout because I wanted to use a custom layout for each row of that list view. When I only have timeSlotListView (i.e. when I comment out the other), it works fine. – MisterM2402 Dec 11 '13 at 14:44
  • @MisterM2402 `android:entries` is supposed to provide pre-defined array of strings. In order to offer custom layout, you need to override the adapter class. – waqaslam Dec 11 '13 at 14:49
  • Oops, just realised I meant that it works fine if I comment out `timeSlotListView`. The tutorial I was following on populating a list view from a database only used a SimpleCursorAdapter (even though a custom layout was used for the list view), but now that I check it again it doesn't seem to say to add an `android:entries` attribute anywhere, not sure where I got that from. – MisterM2402 Dec 11 '13 at 15:08