2016-05-09 50 views
0

我正在嘗試設置上的活動進入我的微調位置,但我得到的logcat這條消息Android的集紡紗位置

嘗試在空對象引用調用虛擬方法void android.widget.Spinner.setSelection(int)

我已經和陣列delcared爲:

<string-array name="genres"> 
    <item>Action</item> 
    <item>Animation</item> 
</string-array> 
在活動

我有

movieGenre = (Spinner) findViewById(R.id.dd_genre); 
    Log.e("ListView set Item", String.valueOf(movieItem.getaNGenre())); 
    movieGenre.setSelection(movieItem.getaNGenre()); 

logcat的顯示我正確的數量:

05-09 12:46:35.624 4940-4940/simpleapps.movierandomizer E/ListView set Item﹕ 3 

但是當我設置的位置,它不會接受的數字。我不明白爲什麼它說「on a null object reference」,當我宣佈它。

+0

的可能的複製[什麼是空指針異常,以及如何解決?(http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-和如何-DO-I-FIX-IT) – Vucko

回答

0

您是否在項目中填充了微調項(可能是本例中的字符串數組?會拋出運行時錯誤。

0

您必須先設置微調器的適配器。設置適配器後,它不會拋出您提到的異常。

Spinner spinner = (Spinner) findViewById(R.id.spinner); 
// Create an ArrayAdapter using the string array and a default spinner layout 

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
      R.array.planets_array, android.R.layout.simple_spinner_item); 

// Specify the layout to use when the list of choices appears 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
// Apply the adapter to the spinner 
spinner.setAdapter(adapter);