Question

I have an activity that displays a list of restaurant dishes. Each view of the list has a number picker associated with it. The application runs fine, however when I start the activity to display the list it crashes...

This is my class file.

NumberPicker np;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_starters);
    createTestData();

    adapter = new MenuItemArrayAdapter(this, starters);
    this.setListAdapter(adapter);

    np = (NumberPicker)findViewById(R.id.numpick);
    np.setMinValue(0);
    np.setMaxValue(99);
}

This is my xml layout file for the StartersActivity class.

<?xml version="1.0" encoding="utf-8"?>

<ListView
    android:id="@+id/android:list"
    android:layout_width="match_parent"
    android:layout_height="670dp">
</ListView>

<TextView
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/empty" />

 <include
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    layout="@layout/pricebar" />

This is my xml layout file for the row in the list.

<?xml version="1.0" encoding="utf-8"?>

<NumberPicker
    android:id="@+id/numpick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" />

<ImageView
    android:id="@+id/dishpic"
    android:layout_width="140dp"
    android:layout_height="150dp"
    android:layout_alignBottom="@+id/numpick"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/item_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/reviewBtn"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/item_price"
    android:text="ITEM NAME"
    android:textSize="20sp" />

<TextView
    android:id="@+id/item_price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/reviewBtn"
    android:layout_below="@+id/item_name"
    android:layout_marginTop="17dp"
    android:layout_toRightOf="@+id/dishpic"
    android:text="ITEM PRICE"
    android:textSize="20sp" />

<Button
    android:id="@+id/reviewBtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/dishpic"
    android:layout_toLeftOf="@+id/numpick"
    android:layout_toRightOf="@+id/dishpic"
    android:text="@string/reviewBtn" />

It seems to be the number picker that is causing the problem because when I take out the following code it works.

np = (NumberPicker)findViewById(R.id.numpick);
    np.setMinValue(0);
    np.setMaxValue(99);

Any ideas as to what might be the problem? The error is a null pointer exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.waitron5/com.example.waitron5.StartersActivity}: java.lang.NullPointerException

I have a feeling it is something to do with trying to access the number picker before the list is loaded and it's not seeing it.

Was it helpful?

Solution 2

Each view of the list has a number picker associated with it

The above statement makes me believe that the NumberPickers controls should be referenced and initialized in the MenuItemArrayAdapter in the getView() method, versus in onCreate() how it is now:

np = (NumberPicker)findViewById(R.id.numpick);
np.setMinValue(0);
np.setMaxValue(99);

OTHER TIPS

To fetch the NumberPicker in the onCreate method it would have to be defined in the main layout. Since it is not you get the NPE.

It depends what you do with the NumberPicker where the best place is to fetch it. It must be specific to the list item view in question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top