Spinner cannot load an integer array?

跟風遠走 提交于 2019-12-09 02:38:34

问题


I have an application, which has a Spinner that I want populated with some numbers (4,8,12,16). I created an integer-array object in strings.xml with the items mentioned above, set the entries of the Spinner to the integer-array, and when I run the app I get:

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)

As soon as I changed the array to a string-array, this works fine. Is this normal? I realize that I can (and will) just convert the string array values to an int, but it seems weird that I have to.

Thanks!

EDIT: Anyone? Anything?


回答1:


What you are attempting to do is not supported.

You likely have some code that looks like this:

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

Which is of course calling the following:

/**
 * 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);
}

The call to getTextArray returns an array with null objects rather than the string representation of the values in your integer array. Digging deeper reveals the source of the problem is in a method of 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;
}

This code assumes you have provided the resource id of an array of strings and thus it is unable to properly extract out values from your array of integers.




回答2:


To overcome this problem, simply put quotes around your int values

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


来源:https://stackoverflow.com/questions/2671657/spinner-cannot-load-an-integer-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!