2010-04-19 70 views
12

我有一個應用程序,它有一個微調,我想填充一些數字(4,8,12,16)。我在與上述物品的strings.xml創建一個整數數組對象,設置Spinner的條目爲整數陣列,並且當我運行應用程序獲取:微調不能加載整數數組?

04-19 23:38:48.016: ERROR/AndroidRuntime(10193): java.lang.NullPointerException 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:198) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.view.View.measure(View.java:7965) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:350) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:278) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.view.View.measure(View.java:7965) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.view.View.measure(View.java:7965) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:464) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:278) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.view.View.measure(View.java:7965) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.view.View.measure(View.java:7965) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.view.ViewRoot.performTraversals(ViewRoot.java:763) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1632) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.os.Looper.loop(Looper.java:123) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at android.app.ActivityThread.main(ActivityThread.java:4310) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at java.lang.reflect.Method.invoke(Method.java:521) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
04-19 23:38:48.016: ERROR/AndroidRuntime(10193):  at dalvik.system.NativeStart.main(Native Method) 

只要我改變該數組爲一個字符串數組,這工作正常。這是正常的嗎?我意識到我可以(也將)只是將字符串數組的值轉換爲一個int,但它似乎很奇怪,我必須。

謝謝!

編輯:任何人?什麼?

+0

嗯,我認爲這將是一個簡單的是/否的答案。 – Adam 2010-04-20 21:01:23

+0

有沒有什麼辦法可以發佈一些代碼。查看代碼比通用的NPE更容易。 – 2010-05-20 23:44:34

回答

25

不支持您正在嘗試執行的操作。

你可能有一些代碼,看起來像這樣:

ArrayAdapter adapter = ArrayAdapter.createFromResource(this, 
    R.array.numbers, android.R.layout.simple_spinner_item); 

這當然是調用以下:

/** 
* Creates a new ArrayAdapter from external resources. The content of the array 
* is obtained through {@link android.content.res.Resources#getTextArray(int)}. 
* 
* @param context The application's environment. 
* @param textArrayResId The identifier of the array to use as the data source. 
* @param textViewResId The identifier of the layout used to create views. 
* 
* @return An ArrayAdapter<CharSequence>. 
*/ 
public static ArrayAdapter<CharSequence> createFromResource(Context context, 
     int textArrayResId, int textViewResId) { 
    CharSequence[] strings = context.getResources().getTextArray(textArrayResId); 
    return new ArrayAdapter<CharSequence>(context, textViewResId, strings); 
} 

getTextArray的調用會返回null對象上的數組,而不是整數數組中值的字符串表示。在深入揭示了問題的根源是在AssetManager方法:

/** 
* Retrieve the text array associated with a particular resource 
* identifier. 
* @param id Resource id of the string array 
*/ 
/*package*/ final CharSequence[] getResourceTextArray(final int id) { 
    int[] rawInfoArray = getArrayStringInfo(id); 
    int rawInfoArrayLen = rawInfoArray.length; 
    final int infoArrayLen = rawInfoArrayLen/2; 
    int block; 
    int index; 
    CharSequence[] retArray = new CharSequence[infoArrayLen]; 
    for (int i = 0, j = 0; i < rawInfoArrayLen; i = i + 2, j++) { 
     block = rawInfoArray[i]; 
     index = rawInfoArray[i + 1]; 
     retArray[j] = index >= 0 ? mStringBlocks[block].get(index) : null; 
    } 
    return retArray; 
} 

此代碼假定您提供一個字符串數組的資源ID,因此它不能正確地從數組中提取出來的值整數。

+1

非常感謝深入的解釋 – Adam 2010-05-24 03:04:34

+0

我讚賞解釋以及。我有一種感覺它沒有得到支持,但我無法找到一個原因 - 現在我有一個。 – Azoreo 2012-02-16 21:59:23

+0

感謝您的澄清。將整數數組分配給佈局文件中的微調器時,我遇到了同樣的問題。 – Hong 2012-12-31 20:29:30

2

爲了克服這個問題,只需把引號的國際價值

<array name="spinner_value"> 
    <item>"18"</item> 
    <item>"8"</item> 
</array>