質問

I am trying to update the value displayed in a TextView when the user changes the value inside the number picker widget.

I have the number picker set up and working correctly however when I attempt to call the TextView's .setText() method I get the following error:

java.lang.NullPointerException at com.example.waitron5.MenuItemArrayAdapter.onValueChange(MenuItemArrayAdapter.java:68)

Below is the code corresponding to where the error seems to be occuring in the MenuItemArrayAdapter class:

@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
    Toast.makeText(this.getContext(), "Change: " + picker.getValue(), Toast.LENGTH_SHORT).show();
    //if oldVal > newVal increment price
    price.setText("xyz");
    //if oldVal < newVal decrement price
}

The Toast message was put in to test the listener was working correctly and it was. I then added the setText() method but that caused the error.

Any help on the issue would be much appreciated! I declare the TextView at the top of the MenuItemArrayAdapter class:

private TextView price;

NOTE: The number picker is contained within each view of a list. Which is why I have the MenuItemAdapter class. Could the problem be linked to trying to update the textView from the wrong location?

Below is the getView() method of the MenuItemAdapter class.

public View getView(final int position, View convertView, ViewGroup parent){
    View v = convertView;

    if(v == null){
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.menuitem_row, null);
    }

    //assign values to view
    MenuItem item = this.menuItems.get(position);

    TextView nameView = (TextView) v.findViewById(R.id.item_name);
    TextView priceView = (TextView) v.findViewById(R.id.item_price);

    nameView.setText(item.getName() + " ");
    priceView.setText("€"+ String.valueOf(item.getPrice()));

    price = (TextView)v.findViewById(R.id.price);

    //number picker
    np = (NumberPicker)v.findViewById(R.id.numpick);
    np.setMaxValue(99);
    np.setMinValue(0);
    np.setValue(0);

    np.setOnValueChangedListener(this);

    return v;

}
役に立ちましたか?

解決

Is the TextView price available in each row of the ListView?
I suppose the answer is no, like you just have a single TextView on the screen that should display the total price.

If my assumption is right, then you sould not instantiate the price in the getView()

price = (TextView)v.findViewById(R.id.price);

as the price will be searched in the R.layout.menuitem_row layout. If the TextView is not there, NullPointer will be thrown when you'll attempt to use.

The solution would be to instantiate the price in the onCreate() where your main layout is declared, and then pass it as a parameter to the MenuItemArrayAdapter.

Something like this:

// ....
TextView price = (TextView)findViewById(R.id.price);
adapter = new MenuItemArrayAdapter(this, price); 
this.setAdapter(adapter)
//....

Then modify the constructor of MenuItemArrayAdapter to accept a TextView:

public MenuItemArrayAdapter(Context context, TextView price){
  this.context = context;
  this.price=price
}

After that you can use safely price to update it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top