【发布时间】: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