2014-01-27 86 views
0

因此,當我嘗試將dropdown3設置爲adapter1時,我得到一個空指針異常。但是,我沒有得到任何關於字符串數組的問題。爲什麼整數數組會發生這種情況?我能做些什麼來解決它?與指針的空指針異常錯誤int數組

public class MainActivity extends Activity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Spinner dropdown1 = (Spinner)findViewById(R.id.spinnertext1); 
    Spinner dropdown2 = (Spinner)findViewById(R.id.spinnertext2); 
    Spinner dropdown3 = (Spinner)findViewById(R.id.spinnernumber1); 
    String[] items = new String[]{"ml", "oz", "L"}; 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items); 
    Integer[] numbers = new Integer[]{1,2,3,4,5,6,7,8,9,10}; 
    ArrayAdapter<Integer> adapter1 = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, numbers); 
     //adapts the string "items" to be an adapter and fit into the spinner. 
    dropdown1.setAdapter(adapter); 
    dropdown2.setAdapter(adapter); 
    dropdown3.setAdapter(adapter1); 

回答

1

我不認爲你可以爲兩個不同的視圖使用相同的適配器對象。 嘗試實例化三個不同的適配器。

+0

哎呀你的權利..我誤解我的日誌錯誤,我把代碼..謝謝! – QuackMan245