1

I'm trying to populate a spinner with a string array using an array adapter on my android application. The code appears sound to me, hence me asking the wonderful community at SO.

Here is my populating function:

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

and my logcat:

09-01 17:17:50.491: E/AndroidRuntime(15264): FATAL EXCEPTION: main 09-01 17:17:50.491: E/AndroidRuntime(15264): android.content.res.Resources$NotFoundException: Resource ID #0x7f090003 type #0x12 is not valid 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2136) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.content.res.Resources.getLayout(Resources.java:865) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.LayoutInflater.inflate(LayoutInflater.java:394) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.widget.Spinner.onMeasure(Spinner.java:439) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.View.measure(View.java:15395) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.View.measure(View.java:15395) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.View.measure(View.java:15395) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.View.measure(View.java:15395) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826) 09-01 17:17:50.491: E/AndroidRuntime(15264): at 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): at android.view.View.measure(View.java:15395) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1985) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1226) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1399) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4553) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.Choreographer.doCallbacks(Choreographer.java:555) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.Choreographer.doFrame(Choreographer.java:525) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.os.Handler.handleCallback(Handler.java:615) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.os.Handler.dispatchMessage(Handler.java:92) 09-01 17:17:50.491: E/AndroidRuntime(15264): at android.os.Looper.loop(Looper.java:137) 09-01 17:17:50.491: E/AndroidRuntime(15264): at 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)

My catch is that I can't seem to find any references to my code (hopefully I'm right there, speaking to my reading skills...), and I'm fairly new at Android so I'm not the best at catching other runtime errors.

Thanks SO!!!

kH

Freestyle076
  • 1,548
  • 19
  • 36

1 Answers1

3

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

The reason for that exception message is that you're supplying a view id to the ArrayAdapter constructor, whereas it's expecting a layout id. You can tell from the getLayout(...) call that is part of the stacktrace, but it could've easily been avoided by properly reading the documentation for the invoked constructor:

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

Parameters
context The current context.
resource The resource ID for a layout file containing a TextView to use when instantiating views.
objects The objects to represent in the ListView.

MH.
  • 45,303
  • 10
  • 103
  • 116
  • Awesome! I see now that this is definitely the problem. Again, I am very new to Android so please bear with me. I've changed the passed value from the spinner's ID to the layout that the spinner is contained in's ID, but now I'm getting an error message telling me I must supply a resource ID for a textView. I'm familiar with textViews and layouts, but I don't see how they work together in this context. Any extra hints? – Freestyle076 Sep 02 '13 at 01:44
  • Okay, just to be clear: you should pass in the layout id for the layout file that contains the `TextView` that will display the data for every item in the `Spinner` (and not the layout file containing the `Spinner` widget). Having done that, make sure the `TextView` in that layout has an id of `@android:id/text1`, so that Android is able to locate it. Alternatively use one of the other `ArrayAdapter` constructors to provide a different id for the `TextView`. Or, slightly more advanced, but also more flexible, create a custom `Adapter` and do all the view and data binding yourself. – MH. Sep 02 '13 at 01:53
  • OK I understand now. I was caught up with the idea of having a TextView as the layout for the spinner. I was stuck on the thought of having to embed the spinner in a TextView or something of the sort. Thank you for the help! – Freestyle076 Sep 03 '13 at 02:15