数字选择器的设置值

时间:2013-02-21 06:05:47

标签: android nullpointerexception numberpicker

我有一个对象的数组列表。每个对象都有一个数量。当用户加载StartersActivity时,我有ListView显示他们可以添加到订单中的商品列表。在每个列表中都有一个NumberPicker

当用户第一次打开活动时,NumberPicker的值设置为0.但是当他们将订单添加到他们的订单时我会增加列表中项目的数量。返回StartersActivity后,我现在希望将NumberPicker的值设置为每个项目的数量。

目前这就是我试图实施他的方式。我打开StartersActivity

时收到此错误

FATAL EXCEPTION:main java.lang.IndexOutOfBoundsException:索引1无效,大小为1

这是在以下位置发生的行错误:

np.setValue(Order.getInstance().getOrderItems().get(position).getQty());

MenuItemArrayAdapter课程:

 public class MenuItemArrayAdapter extends ArrayAdapter<MenuItem> {

//This list holds the items pulled from the web server.
private List<MenuItem> menuItems;
//This list holds the current items the user wants to add to their order.

//Number picker - lets user set quantity of item they wish to order. Adjusts the sub total of the order (price text view).
private static NumberPicker np;

//This text view is updated as user adds/removes items.
private TextView price;
//used to calculate the price.
private double amount = Order.getInstance().getAmount();
private double total = 0.0;

private OrderApplication app;


// Constructor - Takes reference to the Text View price - which will be updated when the value of number picker is adjusted.
public MenuItemArrayAdapter(Context context, List<MenuItem> menuItems,
        TextView price) {
    super(context, R.layout.menuitem_row, menuItems);
    this.menuItems = menuItems;
    this.price = price;

}

public MenuItemArrayAdapter(Context context) {
    super(context, R.layout.menuitem_row);
}

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);
        v.setFocusable(true);
    }

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

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

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

    // number picker
    np = (NumberPicker) v.findViewById(R.id.numpick);
    np.setMaxValue(20);
    np.setMinValue(0);
    if(Order.getInstance().getOrderItems().size()>0){
    np.setValue(Order.getInstance().getOrderItems().get(position).getQty());
    }else{
        np.setValue(0);
    }
    np.setFocusable(false);
    // calculation occurs when values are changed...
    np.setOnValueChangedListener(new OnValueChangeListener() {
        public void onValueChange(NumberPicker picker, int oldVal,
                int newVal) {
            //Add to order - increment price
            if (newVal > oldVal) {
                total = (item.getPrice() * newVal) - item.getPrice()
                        * oldVal;
                Order.getInstance().addToOrder(item);
                item.incQty();
                amount += total;
                Order.getInstance().setAmount(amount);
                Toast.makeText(picker.getContext(), "Item Added: " + item.getName() + " Size: " + Order.getInstance().getOrderItems().size(), Toast.LENGTH_SHORT).show();
            }
            //remove from order - decrement price
            if (newVal < oldVal) {
                total = (item.getPrice() * oldVal) - item.getPrice()
                        * newVal;
        Order.getInstance().removeFromOrder(position);
        item.decQty();
        amount -= total;
    Order.getInstance().setAmount(amount);
    Toast.makeText(picker.getContext(), "Item removed: " + item.getName() + " Size: " + Order.getInstance().getOrderItems().size(), Toast.LENGTH_SHORT).show();

            }
            //update the text view price.
            price.setText("€" + String.valueOf(Order.getInstance().getAmount()));
        }
    });
    return v;
}
 }

任何人都可以解释我的代码出了什么问题。我无法弄清楚。非常感谢。

修改代码以将NumberPicker的值设置为:仍然获得NPE

if(Order.getInstance().getOrderItems().size() > 0){
    //Loop to adjust the number picker to the quantity of dishes ordered.
    for(int i = 0; i != Order.getInstance().getOrderItems().size(); i ++){
        np.setValue(Order.getInstance().getOrderItems().get(position - 1).getQty());
    }
    }else{
        np.setValue(0);
    }

STACK TRACE:

02-21 06:00:51.979: E/AndroidRuntime(6876): FATAL EXCEPTION: main
02-21 06:00:51.979: E/AndroidRuntime(6876):
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 02-21
06:00:51.979: E/AndroidRuntime(6876):     at
java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
02-21 06:00:51.979: E/AndroidRuntime(6876):   at
java.util.ArrayList.get(ArrayList.java:304) 02-21 06:00:51.979:
 E/AndroidRuntime(6876):  at
 com.example.waitronproto9.MenuItemArrayAdapter.getView(MenuItemArrayAdapter.java:84)
 02-21 06:00:51.979: E/AndroidRuntime(6876):  at
 android.widget.AbsListView.obtainView(AbsListView.java:2159) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.widget.ListView.makeAndAddView(ListView.java:1831) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.widget.ListView.fillDown(ListView.java:674) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.widget.ListView.fillFromTop(ListView.java:735) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.widget.ListView.layoutChildren(ListView.java:1652) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.widget.AbsListView.onLayout(AbsListView.java:1994) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.view.View.layout(View.java:14008) 02-21 06:00:51.979:
 E/AndroidRuntime(6876):  at
 android.view.ViewGroup.layout(ViewGroup.java:4373) 02-21 06:00:51.979:
 E/AndroidRuntime(6876):  at
 android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
 02-21 06:00:51.979: E/AndroidRuntime(6876):  at
 android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
 02-21 06:00:51.979: E/AndroidRuntime(6876):  at
 android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.view.View.layout(View.java:14008) 02-21 06:00:51.979:
 E/AndroidRuntime(6876):  at
 android.view.ViewGroup.layout(ViewGroup.java:4373) 02-21 06:00:51.979:
 E/AndroidRuntime(6876):  at
 android.widget.FrameLayout.onLayout(FrameLayout.java:448) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.view.View.layout(View.java:14008) 02-21 06:00:51.979:
 E/AndroidRuntime(6876):  at
 android.view.ViewGroup.layout(ViewGroup.java:4373) 02-21 06:00:51.979:
 E/AndroidRuntime(6876):  at
 android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
 02-21 06:00:51.979: E/AndroidRuntime(6876):  at
 android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
 02-21 06:00:51.979: E/AndroidRuntime(6876):  at
 android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.view.View.layout(View.java:14008) 02-21 06:00:51.979:
 E/AndroidRuntime(6876):  at
 android.view.ViewGroup.layout(ViewGroup.java:4373) 02-21 06:00:51.979:
 E/AndroidRuntime(6876):  at
 android.widget.FrameLayout.onLayout(FrameLayout.java:448) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.view.View.layout(View.java:14008) 02-21 06:00:51.979:
 E/AndroidRuntime(6876):  at
 android.view.ViewGroup.layout(ViewGroup.java:4373) 02-21 06:00:51.979:
 E/AndroidRuntime(6876):  at
 android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
 02-21 06:00:51.979: E/AndroidRuntime(6876):  at
 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
 02-21 06:00:51.979: E/AndroidRuntime(6876):  at
 android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
 02-21 06:00:51.979: E/AndroidRuntime(6876):  at
 android.view.Choreographer.doCallbacks(Choreographer.java:562) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.view.Choreographer.doFrame(Choreographer.java:532) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
 02-21 06:00:51.979: E/AndroidRuntime(6876):  at
 android.os.Handler.handleCallback(Handler.java:725) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.os.Handler.dispatchMessage(Handler.java:92) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 android.os.Looper.loop(Looper.java:137) 02-21 06:00:51.979:
 E/AndroidRuntime(6876):  at
 android.app.ActivityThread.main(ActivityThread.java:5041) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 java.lang.reflect.Method.invokeNative(Native Method) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 java.lang.reflect.Method.invoke(Method.java:511) 02-21 06:00:51.979:
 E/AndroidRuntime(6876):  at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 02-21 06:00:51.979: E/AndroidRuntime(6876):  at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 02-21
 06:00:51.979: E/AndroidRuntime(6876):    at
 dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案