繁体   English   中英

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

[英]android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020014

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

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.id.text1, new String[] { "a", "b"});
setListAdapter(adapter);

这是布局:

<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_list_item_1

new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] { "a", "b"});

为ListView的行定义布局,并调用它,例如, simple_list_item_1 (在文件夹res/layout创建文件simple_list_item_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:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.text1, new String[] { "a", "b"});

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM