我有一個顯示的餐廳菜品列表中的活動。列表中的每個視圖都有一個與其關聯的數字選擇器。該應用程序運行正常,但是當我開始顯示它崩潰列表中的活動...號碼選擇器導致應用程序崩潰
這是我的類文件。
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);
}
這是StartersActivity類我的XML佈局文件。
<?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" />
這是在列表中的行我的XML佈局文件。
<?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" />
這似乎是因爲當我拿出下面的代碼它的工作原理是導致問題的數量選擇器。
np = (NumberPicker)findViewById(R.id.numpick);
np.setMinValue(0);
np.setMaxValue(99);
任何想法可能是什麼問題?該錯誤是一個空指針異常:
了java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.waitron5/com.example.waitron5.StartersActivity}:顯示java.lang.NullPointerException
我有一種感覺,那就是在列表加載之前嘗試訪問數字選擇器並且沒有看到它。
我纔拿到這個權利,你已張貼布局文件是不是'R.layout.activity_starters'? – Henry
對不起,你的佈局文件是列表中行的佈局。我現在將發佈首發佈局。 – Javacadabra