gpt4 book ai didi

android - Spinner 无法加载整数数组?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:34:15 25 4
gpt4 key购买 nike

我有一个应用程序,它有一个微调器,我想用一些数字(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,但我必须这样做似乎很奇怪。

谢谢!

编辑:有人吗?有什么事吗?

最佳答案

不支持您尝试执行的操作。

您可能有一些如下所示的代码:

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 的调用返回一个包含空对象的数组,而不是整数数组中值的字符串表示形式。深入挖掘发现问题的根源在于 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,因此它无法从您的整数数组中正确提取值。

关于android - Spinner 无法加载整数数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2671657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com