Spinner Android的资源NotFoundException

时间:2012-12-29 00:35:22

标签: android android-widget spinner android-arrayadapter android-xml

我有一个有Button的活动。单击Button时,应该打开一个包含TextView和Spinner的简单弹出窗口。我的弹出窗口很好。我的问题是,当我获取Spinner的引用时,它给了我一个Resources $ NotFoundException,并说资源ID无效。

我正在调用popupView.findViewById(R.layout.popup_create_workout)来尝试获取对微调器的引用。我也尝试抓取对弹出窗口使用的父XML布局的引用,并在其上调用finViewById(例如exampleView.findViewById(R.layout.example_popup_view)。我也试过了普通的'findViewById(R.layout.example_popup_view) (我不希望它工作)。但是,这两种技术都会在View ID上或者在Spinner上调用setAdapter()时抛出NullPointerExceptions。我已经尝试在Eclipse中清理项目,仔细检查以确保我正在使用正确的参考ID,上下文等。

让我循环的真正原因是我能够从相同的XML布局中获取对TextView的引用并对其进行操作。我在这个网站上搜索过,发现了一些类似的问题,但没有一个是我需要的。我也用Google搜索了所有我想不到的运气。

所以,我的qeustion是:当我尝试获取对微调器的引用时,为什么我得到一个Resources $ NotFoundException?我知道这可能很容易,但我似乎无法弄明白。在此先感谢您的帮助!

以下是与微调器有关的活动部分:

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

});

XML布局:

<?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>

最后,堆栈跟踪:

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)

这是包含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>

2 个答案:

答案 0 :(得分:2)

我不确定导致异常的原因,但可能是这个。变化:

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

要:

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

ArrayAdapter已经需要一个ArrayList,你只需要提供ArrayList的子类型。


根本问题
将您的布​​局文件命名为与View的ID相同的名称存在轻微危险......您已找到它。您希望在初始化ArrayAdapter时使用R.layout.list_content_white 而不是 R.id.list_content_white

答案 1 :(得分:0)

将它放在onCreate()

中的两行中
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_add_goal, null);