使用字符串数组填充Spinner

时间:2013-09-02 00:23:06

标签: android android-arrayadapter android-spinner

我试图在我的Android应用程序上使用数组适配器填充带有字符串数组的微调器。代码对我来说听起来很合理,因此我在SO上询问了精彩的社区。

这是我的填充功能:

private void populateSpinners(String intentMessage)
{
//initialize variables and get spinners, resources
Resources res = getResources();
String[] types = null;
Spinner iHaveSpinner = (Spinner) findViewById(R.id.iHaveSpinner);
Spinner iWantSpinner = (Spinner) findViewById(R.id.iWantSpinner);

//grab appropriate array for unit types
if(intentMessage.equalsIgnoreCase("Force"))
    types = res.getStringArray(R.array.ForceArray);

//initialize adapter and populate spinners
ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,R.id.iHaveSpinner,types);
iHaveSpinner.setAdapter(typeAdapter);
iWantSpinner.setAdapter(typeAdapter);
}

和我的logcat:

  

09-01 17:17:50.491:E / AndroidRuntime(15264):致命异常:主要   09-01 17:17:50.491:E / AndroidRuntime(15264):android.content.res.Resources $ NotFoundException:资源ID#0x7f090003类型#0x12无效   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.content.res.Resources.loadXmlResourceParser(Resources.java:2136)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.content.res.Resources.getLayout(Resources.java:865)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.LayoutInflater.inflate(LayoutInflater.java:394)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.widget.Spinner.onMeasure(Spinner.java:439)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.View.measure(View.java:15395)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:617)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.View.measure(View.java:15395)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.View.measure(View.java:15395)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.widget.LinearLayout.measureVertical(LinearLayout.java:833)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.widget.LinearLayout.onMeasure(LinearLayout.java:574)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.View.measure(View.java:15395)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)   09-01 17:17:50.491:E / AndroidRuntime(15264):at com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2362)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.View.measure(View.java:15395)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1985)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1226)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1399)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4553)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.Choreographer.doCallbacks(Choreographer.java:555)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.Choreographer.doFrame(Choreographer.java:525)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:711)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.os.Handler.handleCallback(Handler.java:615)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.os.Handler.dispatchMessage(Handler.java:92)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.os.Looper.loop(Looper.java:137)   09-01 17:17:50.491:E / AndroidRuntime(15264):在android.app.ActivityThread.main(ActivityThread.java:4950)   09-01 17:17:50.491:E / AndroidRuntime(15264):at java.lang.reflect.Method.invokeNative(Native Method)   09-01 17:17:50.491:E / AndroidRuntime(15264):at java.lang.reflect.Method.invoke(Method.java:511)   09-01 17:17:50.491:E / AndroidRuntime(15264):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1004)   09-01 17:17:50.491:E / AndroidRuntime(15264):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)   09-01 17:17:50.491:E / AndroidRuntime(15264):at dalvik.system.NativeStart.main(Native Method)

我的注意事项是,我似乎无法找到任何对我的代码的引用(希望我就在那里,对我的阅读技巧说话......),而且我还是比较陌生的Android因此我不能最好地捕捉其他运行时错误。

非常感谢!!!

的kH

1 个答案:

答案 0 :(得分:3)

Resource ID #0x7f090003 type #0x12 is not valid

该异常消息的原因是您向ArrayAdapter构造函数提供视图ID ,而期望布局ID 。你可以从作为堆栈跟踪一部分的getLayout(...)调用来判断,但通过正确阅读documentation for the invoked constructor可以很容易地避免它:

public ArrayAdapter (Context context, int resource, T[] objects)
  

参数
  context当前背景。
  resource 布局文件的资源ID ,包含在实例化视图时使用的TextView。
  objects要在ListView中表示的对象。