1

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

我目前正在调用 popupView.findViewById(R.layout.popup_create_workout) 以尝试获取对微调器的引用。我还尝试获取对弹出窗口使用的父 XML 布局的引用并在其上调用 finViewById(例如 exampleView.findViewById(R.layout.example_popup_view)。我还尝试了普通的 ol'findViewById(R.layout.example_popup_view) (我没想到会起作用。但是,这两种技术都会在 View ID 上或当我在 Spinner 上调用 setAdapter() 时抛出 NullPointerExceptions。我尝试在 Eclipse 中清理项目,仔细检查以确保我正在使用正确的参考 ID、上下文等。

真正让我陷入循环的是,我能够从相同的 XML 布局中获取对 TextView 的引用并对其进行操作。我搜索了整个网站,发现了一些类似的问题,但没有一个是我所需要的。我还用谷歌搜索了我能想到的一切,但没有运气。

所以,我的问题是:当我尝试获取微调器的引用时,为什么会得到 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>
4

2 回答 2

3

我不确定是什么导致了异常,但可能是这样。改变:

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 的子类型。


根本问题
将布局文件命名为与视图 id 相同的名称有一点危险……你已经找到了。您不想R.layout.list_content_white R.id.list_content_white初始化 ArrayAdapter 时使用。

于 2012-12-29T00:38:51.463 回答
0

把它放在 onCreate() 的两行中

LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_add_goal, null);
于 2012-12-29T04:44:25.133 回答