微调无法加载一个整数数组? [英] Spinner cannot load an integer array?

查看:149
本文介绍了微调无法加载一个整数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它有,我想填充了一些数字(4,8,12,16)一个微调。我的strings.xml与上述项目创建一个整数数组对象,设置微调的条目的整数数组,当我运行该应用程序我得到:

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)

当我改变了数组为字符串数组,这工作正常。这正常吗?我知道我可以(并且)只是字符串数组值转换为int,但它似乎不可思议,我不得不这样做。

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.

谢谢!

编辑:有人吗?什么?

推荐答案

您正试图做的是不支持的。

您可能有一些code,它是这样的:

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

在调用 getTextArray 返回与空对象,而不是字符串重新$ P $中值的整数数组psentation阵列。进一步挖掘揭示了问题的根源是在一个方法 AssetManager

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;
}

这code假设你提供一个字符串数组的资源ID,因此它不能正确地从整数数组中提取出来的值。

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.

这篇关于微调无法加载一个整数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆