【问题标题】:Outputting ListView exception: File from xml type layout resource ID #0x102000a输出 ListView 异常:来自 xml 类型布局资源 ID #0x102000a 的文件
【发布时间】:2012-04-14 12:41:20
【问题描述】:

我正在尝试从 SQLite 数据库中输出一个列表,但遇到了这个异常:

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

在检查了类似问题后,我确定我的 ListView 被定义为 @id/android:list 但仍然得到相同的异常。当 ListActivity 类中的 onCreate 方法完成时,就会出现问题。 getAllDiaryEntries() 方法确实从数据库中获取数据,但同样,一旦它返回 ListActivity 类并且 onCreate 完成,我就会得到异常。这是代码的相关部分。

这是 ListActivity 类:

public class DiarySchedule extends ListActivity
{
private DiaryDataSource datasource;
private static final String TAG = "DiaryDbAdapter";

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.diary_schedule);

    datasource = new DiaryDataSource(this);
    datasource.open();

    List<DiaryEntry> values = datasource.getAllDiaryEntries();

    ArrayAdapter<DiaryEntry> adapter = new ArrayAdapter<DiaryEntry>(this,
            android.R.id.list, values);
    setListAdapter(adapter);
}

接下来我们有 getAllDiaryEntries() 方法:

public List<DiaryEntry> getAllDiaryEntries() 
{
    List<DiaryEntry> diaryEntries = new ArrayList<DiaryEntry>();

    Cursor cursor = database.query(DiaryDbAdapter.DIARY_TABLE,
            allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) 
    {
        DiaryEntry diaryEntry = cursorToDiaryEntry(cursor);
        diaryEntries.add(diaryEntry);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return diaryEntries;
}

还有布局(还没有样式或任何东西):

<ListView
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

我对 Android 还是很陌生,所以可能错过了一些简单的东西,但在此先感谢您的帮助。

已编辑 - 这是 cursortoDiaryEntry 方法

private DiaryEntry cursorToDiaryEntry(Cursor cursor) { DiaryEntry diaryEntry = new DiaryEntry(); diaryEntry.setId(cursor.getLong(0)); diaryEntry.setTitle(cursor.getString(1)); diaryEntry.setDate(cursor.getString(2)); diaryEntry.setDescription(cursor.getString(3)); 返回日记条目; }

【问题讨论】:

  • 感谢 Barak 的建议 - 我会确保始终提供反馈

标签: android listview listactivity


【解决方案1】:

ArrayAdapter 的构造函数(你正在使用的那个)定义如下:

public ArrayAdapter (Context context, int textViewResourceId, List<T> objects)

这里的 textViewResourceId 是“包含 TextView 的布局文件的资源 ID,以在实例化视图时使用。”。见here

您应该使用 R.layout.layout_file 传递一个包含 &lt;TextView/&gt; 元素的布局文件。

【讨论】:

  • 谢谢丹尼斯。异常现在已经消失,但出现了其他问题..!更改为:'ArrayAdapter adapter = new ArrayAdapter(this,R.layout.diary_schedule, values);'其中'diary_schedule'是我的布局文件,弹出一个异常需要TextView的ID,所以我再次添加:'ArrayAdapter adapter = new ArrayAdapter(this, R.layout.diary_schedule, R. id.scheduleList,值);'其中 'schedule_list' 是 TextView,现在没有例外,但页面出现空白..
  • 好吧,这很有趣 - 我刚刚尝试将其更改为 'ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values);'它输出 2 行,但它输出包名称,然后输出“DiaryEntry@412f7728”(DiaryEntry 是我的类,带有 getTitle、getDate 等方法)。有任何想法吗?我在某处公然做了一些非常愚蠢的事情!
  • 听起来您创建了一个指针列表,而不是它们指向的对象列表。也许把你的cursorToDiaryEntry 方法给我们看看。
  • 在第一条评论中,您使用 R.id.scheduleList 作为 TextView id,但您提到 TextView 的 id 是“schedule_list”。在这两种情况下,它都会尝试将 TextView 的值设置为对象 DiaryEntry。它调用 toString() 方法将对象转换为字符串(即 DiaryEntry@412f7728)。您可以覆盖 DiaryEntry 类中的 toString() 方法以打印正确的信息,但最好创建一个扩展 ArrayAdapter 的新类,然后覆盖 getView() 方法。在此处查看示例:stackoverflow.com/a/10153102/1327789
  • 好的,我已经在上面添加了 cursorToDiaryEntry 方法,我将尝试创建一个扩展 ArrayAdapter 的新类,并让您知道我是如何进行的。再次感谢大家!
【解决方案2】:

dennisg 有正确的答案来解决您的问题。

我可以建议您通过将光标变成数组来做额外的工作吗?您可以通过直接使用光标来创建列表适配器来节省一些周期。

Cursor cursor = database.query(DiaryDbAdapter.DIARY_TABLE,
        allColumns, null, null, null, null, null);
getActivity().startManagingCursor(cursor);

String[] from = new String[] { DiaryDbAdapter.DIARY_ENTRY };
int[] to = new int[] {  R.id.ListItem1 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
    R.layout.listlayoutdouble, cursor, from, to);
setListAdapter(adapter);

您的 from string[] 包含您希望包含在显示中的列列表,to int[] 包含您希望将数据放入的资源 id(与 from 数组的顺序匹配)。然后适配器标注指定使用哪个行布局、哪个游标以及将数据映射到视图的两个数组。

【讨论】:

  • 我尝试单击此 Barak 上的向上箭头,但显然我还没有足够高的声誉来执行此操作!不过再次感谢!
猜你喜欢
  • 2020-02-02
  • 1970-01-01
  • 1970-01-01
  • 2017-08-25
  • 2020-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-24
相关资源
最近更新 更多