资源$ NotFoundException for android.R.layout.simple_list_item_1

时间:2011-07-04 14:15:33

标签: android android-layout

我正在阅读Arno Becker和Marcus Pant的“Android 2”一书。在本书的过程中,他们与读者一起开发了一个小型的android应用程序。 有一点我现在得到一个我无法解决的错误。 我有一个ListActivity,应该填充静态数据。每当我启动活动时,我都会收到一个异常资源$ NotFoundException,其中包含文本:“xml类型布局资源ID中的文件res / layout / simple_list_item_1.xml”。

以下是调用活动的代码:

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

以下是活动的代码:

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 /布局/ 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)时,错误消息从上面提到的消息(文件res / layout / simple_list_item_1.xml从xml类型布局资源ID)更改为“ERROR / AndroidRuntime(2609):android.content.res.Resources $ NotFoundException:文件res / layout / typing_filter.xml来自xml类型布局资源ID#0x1090069”。这个错误更具抵抗力。将typing_filter.xml复制到我自己的项目目录中不会改变任何内容。

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

编辑: 我刚看到LogCat打印出比标准eclipse视图更有用的堆栈跟踪。 在抛出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

EDIT2: 感谢@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模拟器。

1 个答案:

答案 0 :(得分:3)

列表活动只有在您的列表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