gpt4 book ai didi

android - android.R.layout.simple_list_item_1 的资源 $NotFoundException

转载 作者:行者123 更新时间:2023-11-29 18:18:55 25 4
gpt4 key购买 nike

我正在阅读 Arno Becker 和 Marcus Pant 合着的《Android 2》一书。在本书的类(class)中,他们与读者一起开发了一个小型安卓应用程序。现在有一次我遇到了一个我无法解决的错误。我有一个 ListActivity 应该填充静态数据。每当我开始 Activity 时,我都会收到一个异常 Resources$NotFoundException,其文本为:“File res/layout/simple_list_item_1.xml from xml type layout resource ID”。

这是调用 Activity 的代码:

public void onClickManageContacts(View view)
{
final Intent intent=new Intent(this,ListContacts.class);
startActivity(intent);
}

这是 Activity 的代码:

package androidbook.amando.gui;

import androidbook.amando.R;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class ListContacts extends ListActivity {

private static final String[] NAMES = new String[] { "Berthold Schmitz",
"Chantal Schulze", "Bartolomäus Weissenbaum", "Jean-Paul Küppers" };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.geokontakte_auflisten);

showContacts();
}

private void showContacts() {
final ArrayAdapter<String> contactAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, NAMES);
setListAdapter(contactAdapter);
}
}

res/layout/geokontakte_auflisten.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:id="@+id/sp_sortierung"
android:layout_width="wrap_content"
android:entries="@array/Sortierung">
</Spinner>
<ListView
android:layout_height="wrap_content"
android:id="@+id/android:list"
android:layout_width="wrap_content"
android:textFilterEnabled="true"
android:cacheColorHint="@color/hintergrund">
</ListView>
<TextView
android:id="@+id/android:empty"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/txt_geokontakt_auflisten_keineDaten">
</TextView>
</LinearLayout>

抛出异常的时间是在 showContacts() 方法完成之后。这里是堆栈跟踪:

Thread [<1> main] (Suspended (exception Resources$NotFoundException))   
Resources.loadXmlResourceParser(int, String) line: 1877
Resources.getLayout(int) line: 731
PhoneLayoutInflater(LayoutInflater).inflate(int, ViewGroup, boolean) line: 318
ArrayAdapter.createViewFromResource(int, View, ViewGroup, int) line: 332
ArrayAdapter.getView(int, View, ViewGroup) line: 323
ListView(AbsListView).obtainView(int, boolean[]) line: 1430
ListView.measureHeightOfChildren(int, int, int, int, int) line: 1216
ListView.onMeasure(int, int) line: 1127
ListView(View).measure(int, int) line: 8313
LinearLayout(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 3138
LinearLayout.measureChildBeforeLayout(View, int, int, int, int, int) line: 1017
LinearLayout.measureVertical(int, int) line: 386
LinearLayout.onMeasure(int, int) line: 309
LinearLayout(View).measure(int, int) line: 8313
FrameLayout(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 3138
FrameLayout.onMeasure(int, int) line: 250
FrameLayout(View).measure(int, int) line: 8313
PhoneWindow$DecorView(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 3138
PhoneWindow$DecorView(FrameLayout).onMeasure(int, int) line: 250
PhoneWindow$DecorView(View).measure(int, int) line: 8313
ViewRoot.performTraversals() line: 839
ViewRoot.handleMessage(Message) line: 1859
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 3683
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 839
ZygoteInit.main(String[]) line: 597
NativeStart.main(String[]) line: not available [native method]

当我注释掉 setListAdapter() 行时,没有抛出异常。当给定数组为空且没有要显示的列表项时,也不会抛出异常。

当我将SDK中的simple_list_item_1.xml文件复制到我自己的项目(res/layout)中时,错误信息从上面提到的信息(File res/layout/simple_list_item_1.xml from xml type layout resource ID)变为“错误/AndroidRuntime(2609):android.content.res.Resources$NotFoundException:来自 xml 类型布局资源 ID #0x1090069 的文件 res/layout/typing_filter.xml”。这个错误更能抵抗。将 typing_filter.xml 复制到我自己的项目目录中不会改变任何内容。

我也在论坛 anddev.org 上问过这个问题,但没有得到答案,现在意识到那里的回答率不高。

编辑:我刚刚看到 LogCat 打印出比标准 eclipse View 更有用的堆栈跟踪。在抛出 Resources$NotFoundException 之前似乎有一个 FileNotFound 异常:

06-25 19:41:40.844: ERROR/AndroidRuntime(2802): FATAL EXCEPTION: main
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): android.content.res.Resources$NotFoundException: File res/layout/simple_list_item_1.xml from xml type layout resource ID #0x1090003
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1916)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1871)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.content.res.Resources.getLayout(Resources.java:731)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.widget.ListView.onMeasure(ListView.java:1127)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.view.View.measure(View.java:8313)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.view.View.measure(View.java:8313)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.view.View.measure(View.java:8313)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.view.View.measure(View.java:8313)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.os.Looper.loop(Looper.java:123)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at java.lang.reflect.Method.invoke(Method.java:507)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at dalvik.system.NativeStart.main(Native Method)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): Caused by: java.io.FileNotFoundException: res/layout/simple_list_item_1.xml
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.content.res.AssetManager.openXmlAssetNative(Native Method)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:486)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1898)
06-25 19:41:40.844: ERROR/AndroidRuntime(2802): ... 30 more

编辑2:感谢@DArkO 的快速回答。我更改了 res/layout/geokontakte_auflisten.xml,但我得到了完全相同的错误。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:id="@+id/sp_sortierung"
android:layout_width="wrap_content"
android:entries="@array/Sortierung">
</Spinner>
<ListView
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_width="wrap_content"
android:textFilterEnabled="true"
android:cacheColorHint="@color/hintergrund">
</ListView>
<TextView
android:id="@android:id/empty"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/txt_geokontakt_auflisten_keineDaten">
</TextView>
</LinearLayout>

我使用的是 API 版本 10 和 android 2.3.3 模拟器。

最佳答案

列表 Activity 仅在您的列表 ID 为以下时才支持:

android:id="@android:id/list"

只需在这里替换那个:

 <ListView
android:layout_height="wrap_content"
android:id="@+id/android:list"
android:layout_width="wrap_content"
android:textFilterEnabled="true"
android:cacheColorHint="@color/hintergrund">

查看此 link :

关于android - android.R.layout.simple_list_item_1 的资源 $NotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6572658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com