Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android.content.res.Resources$NotFoundException:来自xml类型布局资源ID#0x1020014的文件_Java_Android_Android Layout_Listactivity_Android Resources - Fatal编程技术网

Java android.content.res.Resources$NotFoundException:来自xml类型布局资源ID#0x1020014的文件

Java android.content.res.Resources$NotFoundException:来自xml类型布局资源ID#0x1020014的文件,java,android,android-layout,listactivity,android-resources,Java,Android,Android Layout,Listactivity,Android Resources,我正在尝试在ListActivity中使用ArrayAdapter: ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.id.text1, new String[] { "a", "b"}); setListAdapter(adapter); ArrayAdapter adapter=newarrayadapter(这个,android.R.id.text1,新字符串[]{“a

我正在尝试在ListActivity中使用ArrayAdapter:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.id.text1, new String[] { "a", "b"});
setListAdapter(adapter);
ArrayAdapter adapter=newarrayadapter(这个,android.R.id.text1,新字符串[]{“a”,“b”});
setListAdapter(适配器);
这是布局图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@id/android:text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" />

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="false"
        android:layout_below="@id/android:text1" >

    </ListView>

</RelativeLayout>


我在StackOverflow上找到的解决方案似乎都不起作用。我使用的是API 10。您能帮忙吗?

您使用的资源类型错误,您需要参考
R.layout
而不是
R.id
。试试android.R.layout.simple\u list\u item\u 1:

new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] { "a", "b"});
newarrayadapter(这是android.R.layout.simple_list_item_1,新字符串[]{“a”,“b”});

为ListView的行定义一个布局,并将其称为,例如,
简单列表项目项目项目1
(在文件夹
res/layout
中创建文件
简单列表项目项目1.xml
)。将您的
TextView
放入此布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@id/android:text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" />
</RelativeLayout>

然后像这样创建阵列适配器:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.text1, new String[] { "a", "b"});
ArrayAdapter adapter=new ArrayAdapter(这个,R.layout.simple_list_item_1,R.id.text1,新字符串[]{“a”,“b”});

您将找到有关列表和适配器的详细说明。

否,文档很清楚,第二个参数是布局。也许您正在考虑:
ArrayAdapter(上下文上下文、int资源、int textViewResourceId、T[]对象)
其中第三个参数是TextView的id。哇,您说得对!我怎么会错过呢?这实际上是解决问题的最简单方法。很遗憾,我不能同时接受这两个答案。这解决了问题-也许不像Sam的建议那么简单,但你也向我展示了如何自定义列表项。非常感谢。