I am reading the book "Android 2" from Arno Becker and Marcus Pant. In the course of this book they develop a small android application with the reader. At one point now I get an error I can't solve. I have an ListActivity which should be filled with static data. Whenever I start the activity, I get an Exception Resources$NotFoundException with the text: "File res/layout/simple_list_item_1.xml from xml type layout resource ID".
Here is the code calling the activity:
public void onClickManageContacts(View view)
{
final Intent intent=new Intent(this,ListContacts.class);
startActivity(intent);
}
Here is the code of the 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/androi开发者_Python百科d: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>
The time when the exception is thrown, is after the method showContacts() finished. Here the stack trace:
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]
The exception is not thrown, when I comment out the setListAdapter() line. The exception is also not throw, when the given array is empty and there are no list items to be displayed.
When I copy the simple_list_item_1.xml file from the SDK into my own project (res/layout), The error message changes from the above mentioned message (File res/layout/simple_list_item_1.xml from xml type layout resource ID) to "ERROR/AndroidRuntime(2609): android.content.res.Resources$NotFoundException: File res/layout/typing_filter.xml from xml type layout resource ID #0x1090069". This error is more resistant. Copying typing_filter.xml into my own projects directory doesn't change anything.
I also asked this question in the forum anddev.org, but didn't get an answer and now realized that the answer rates there aren't high.
edit: I just saw that LogCat prints a more useful stacktrace than the standard eclipse view. There seems to be a FileNotFound exception before Resources$NotFoundException is thrown:
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: Thank you @DArkO for your quick answer. I changed res/layout/geokontakte_auflisten.xml, but I am getting exactly the same error.
<?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>
I am using API version 10 and an android 2.3.3 emulator.
A list activity can only support if your list's id is:
android:id="@android:id/list"
just replace that one here:
<ListView
android:layout_height="wrap_content"
android:id="@+id/android:list"
android:layout_width="wrap_content"
android:textFilterEnabled="true"
android:cacheColorHint="@color/hintergrund">
See this link:
精彩评论