1

I have an activity that has a Button. When the Button is clicked it is supposed to open a simple Popup Window that contains a TextView and a Spinner. I have the Popup Window opening up fine. My problem is when I grab a reference to the Spinner it's giving me a Resources$NotFoundException and saying that the resource ID is not valid.

I'm currently calling popupView.findViewById(R.layout.popup_create_workout) to try to get a reference to the spinner. I've also tried grabbing a reference to the parent XML layout that the Popup Window uses and invoking finViewById on it (e.g. exampleView.findViewById(R.layout.example_popup_view). I also tried plain ol' findViewById(R.layout.example_popup_view) (which I wasn't expecting to work). However, both of these techniques throw NullPointerExceptions either on the View ID or when I call setAdapter() on the Spinner. I've tried cleaning the project in Eclipse, double checked to make sure the I'm using the right reference IDs, context, etc.

What's really throwing me for a loop is that I am able to get references to the TextView from the same XML layout and manipulate it. I've searched all over this site and found somewhat similar questions, but none of them were quite what I needed. I've also Googled everything that I could think of with no luck.

So, my qeustion is: why am I getting a Resources$NotFoundException when I try to get a reference to the spinner? I know it's probably something easy, but I can't seem to figure it out. Thanks in advance for your help!

Here's the part of the activity that has to do with the spinner:

@Override
public void onClick(View v) {
    LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.popup_add_goal, null);
    PopupWindow popupWindow = new PopupWindow(popupView);
    popupWindow.setWidth(240);
    popupWindow.setHeight(447);
    popupWindow.update();
    popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);

    ArrayList<String> list = new ArrayList<String>();
    list.add("Item 1");
    list.add("Item 2");
    list.add("Item 3");

    Spinner spinner = (Spinner) popupView.findViewById(R.id.goal_spinner);
    ArrayAdapter<ArrayList<String>> arrayAdapter = new ArrayAdapter<ArrayList<String>>(WorkoutActivity.context, R.id.list_content_white);
    arrayAdapter.add(list);
    spinner.setAdapter(arrayAdapter);
}

});

And the XML layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#CCff0000" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Add goals"
    android:textColor="#ffffff"
    android:textSize="48sp" />

<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Search for a goal"
        android:textColor="#ffffff"
        android:textSize="40sp"
        android:gravity="center" />

    <Spinner android:id="@+id/goal_spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff" />

</LinearLayout>

And finally, the stack trace:

12-29 03:51:14.832: E/AndroidRuntime(5216): FATAL EXCEPTION: main
12-29 03:51:14.832: E/AndroidRuntime(5216): android.content.res.Resources$NotFoundException: Resource ID #0x7f09001a type #0x12 is not valid
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2144)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.content.res.Resources.getLayout(Resources.java:853)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.widget.Spinner.onMeasure(Spinner.java:439)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.View.measure(View.java:15513)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.View.measure(View.java:15513)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.View.measure(View.java:15513)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.Choreographer.doFrame(Choreographer.java:532)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.os.Handler.handleCallback(Handler.java:725)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.os.Handler.dispatchMessage(Handler.java:92)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.os.Looper.loop(Looper.java:137)
12-29 03:51:14.832: E/AndroidRuntime(5216): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-29 03:51:14.832: E/AndroidRuntime(5216): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 03:51:14.832: E/AndroidRuntime(5216): at java.lang.reflect.Method.invoke(Method.java:511)
12-29 03:51:14.832: E/AndroidRuntime(5216): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-29 03:51:14.832: E/AndroidRuntime(5216): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-29 03:51:14.832: E/AndroidRuntime(5216): at dalvik.system.NativeStart.main(Native Method)

Here's the layout that contains list_content_white:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:id="@+id/list_content_white"
android:textColor="#ffffff"
android:textSize="30sp"
android:textStyle="bold"
android:layout_margin="8dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>
Don
  • 506
  • 2
  • 8
  • 23
  • What line does the _LogCat trace_ point to? – K-ballo Dec 29 '12 at 00:36
  • Um, it's actually not giving me a specific line (not that I see anyway). I put the whole stack trace up so that you can go ahead and take a look. – Don Dec 29 '12 at 04:17

2 Answers2

3

I'm not sure what is causing the exception, but it could be this. Change:

ArrayAdapter<ArrayList<String>> arrayAdapter = new ArrayAdapter<ArrayList<String>>(WorkoutActivity.context, R.id.list_content_white);
arrayAdapter.add(list);

To:

ArrayAdapter<String> arrayAdapter = 
    new ArrayAdapter<String>(WorkoutActivity.this, R.id.list_content_white, list);
//                        This is wrong, see below  ^^^^

The ArrayAdapter already expects an ArrayList, you simply need to provide the ArrayList's subtype.


The root problem
There is a slight danger in naming your layout file the same name as a View's id... You have found it. You want to use R.layout.list_content_white not R.id.list_content_white while initializing your ArrayAdapter.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • You're right. It didn't fix the exception, but it made my code a little cleaner. Thanks! – Don Dec 29 '12 at 04:03
  • I updated my answer. Did you try `WorkoutActivity.this` instead of `WorkoutActivity.context`? – Sam Dec 29 '12 at 04:33
  • Ok. I tried WorkoutPlannerPopup.this (which is the name of the class that holds this activity) and gestBaseContext(), but neither one fixed the problem... – Don Dec 29 '12 at 05:09
  • "I tried WorkoutPlannerPopup.this (which is the name of the class that holds this activity)" Look at my latest (hopefully last) update that uses `R.layout.list_content_white`, but you may need to use `WorkoutPlannerPopup.this` as well. – Sam Dec 29 '12 at 05:19
  • The names are actually different. Sorry, I should have clarified that. The layout XML file name is list_layout_white_text and the TextView in it has the id list_content_white. Also, the ArrayAdapter is throwing an error when I supply a layout view because it needs a TextView instead. – Don Dec 29 '12 at 05:58
  • Progress! :) That is a different type of error. Without seeing the error I know the answer is to use this constructor: `new ArrayAdapter(WorkoutPlannerPopup.this, R.layout.list_layout_white_text, R.id.list_content_white, list);`. When you pass a complex layout like yours, you must _also_ tell the ArrayAdapter the TextView's id. – Sam Dec 29 '12 at 06:23
  • That did the Trick! I can't believe that's what it was. I've read so many times that if you need a more complex layout that you have to do what you just suggested. I guess I just forgot. Thanks so much! – Don Dec 29 '12 at 06:30
  • You're welcome. Please click the check mark to accpet this answer as the solution. Good luck! – Sam Dec 29 '12 at 06:33
0

Put this in two lines in onCreate()

LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_add_goal, null);
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
  • This code is actually already in the onCreateMethod. The onClickMethod that I posted is contained in the onCreate method in a Button.OnClickListener method. – Don Dec 29 '12 at 05:27