我使用SimpleCursorAdapter创建了一个ListView来显示数据库内容。
我多次测试并发现SimpleCursorAdapter部分的初始化是个问题。 (我看到了DB内容跟随Toast消息。)
布局和ID也与xml文件完全匹配......
你能告诉我可能是什么问题吗? ('弃用'会出问题吗?)
private void populateListViewFromDB()
{
Cursor cursor = dbAdapter.getAllRows();
ListView myList = (ListView)findViewById(R.id.listView);
startManagingCursor(cursor);
String[] fromFieldNames = new String[]
{dbAdapter.KEY_DATE, dbAdapter.KEY_CATEGORY, dbAdapter.KEY_COST,
dbAdapter.KEY_EXPLANATION};
int[] toViewIds = new int[]
{R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4};
/*
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
this, // Context
R.layout.cash_expense_list_layout, // Row layout template
cursor, // cursor (set of DB records to map)
fromFieldNames, // DB Column names
toViewIds // View IDs to put information in
);
//Set the adapter for the listView
myList.setAdapter(myCursorAdapter);
*/
cursor.moveToFirst();
// move to the next item each time
while (cursor.moveToNext()) {
// get string and column index from cursor
String date = cursor
.getString(cursor.getColumnIndex(dbAdapter.KEY_DATE));
String category = cursor.getString(cursor
.getColumnIndex(dbAdapter.KEY_CATEGORY));
String explanation = cursor.getString(cursor
.getColumnIndex(dbAdapter.KEY_EXPLANATION));
int cost = cursor.getInt(cursor.getColumnIndex(dbAdapter.KEY_COST));
Toast.makeText(this,
"Date: " + date + "\n" + "Category: " + category + " Cost : " + cost,
Toast.LENGTH_SHORT).show();
}
下面是logcat
11-18 15:34:24.405: E/AndroidRuntime(2730): FATAL EXCEPTION: main
11-18 15:34:24.405: E/AndroidRuntime(2730): Process: com.example.money, PID: 2730
11-18 15:34:24.405: E/AndroidRuntime(2730): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.money/com.example.money.CashAnalysis}: java.lang.IllegalArgumentException: column '_id' does not exist
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.os.Handler.dispatchMessage(Handler.java:102)
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.os.Looper.loop(Looper.java:136)
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.app.ActivityThread.main(ActivityThread.java:5001)
11-18 15:34:24.405: E/AndroidRuntime(2730): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 15:34:24.405: E/AndroidRuntime(2730): at java.lang.reflect.Method.invoke(Method.java:515)
11-18 15:34:24.405: E/AndroidRuntime(2730): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-18 15:34:24.405: E/AndroidRuntime(2730): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-18 15:34:24.405: E/AndroidRuntime(2730): at dalvik.system.NativeStart.main(Native Method)
11-18 15:34:24.405: E/AndroidRuntime(2730): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.widget.CursorAdapter.init(CursorAdapter.java:172)
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.widget.CursorAdapter.<init>(CursorAdapter.java:120)
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52)
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
11-18 15:34:24.405: E/AndroidRuntime(2730): at com.example.money.CashAnalysis.populateListViewFromDB(CashAnalysis.java:35)
11-18 15:34:24.405: E/AndroidRuntime(2730): at com.example.money.CashAnalysis.onCreate(CashAnalysis.java:19)
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.app.Activity.performCreate(Activity.java:5231)
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-18 15:34:24.405: E/AndroidRuntime(2730): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
11-18 15:34:24.405: E/AndroidRuntime(2730): ... 11 more
答案 0 :(得分:0)
不,弃用不会导致此问题。
此错误清楚地告诉您问题的确切原因 - 您的数据库不包含名为_id
的列,但此列是必需的
11-18 15:34:24.405: E/AndroidRuntime(2730): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
此行指向导致上述异常的代码库的确切行
11-18 15:34:24.405: E/AndroidRuntime(2730): at com.example.money.CashAnalysis.populateListViewFromDB(CashAnalysis.java:35)
有关为何需要此列的详细信息,请参阅this Android documentation,其中包含以下注释:
注意:要使用Cursor备份ListView,游标必须包含a 列名为_ID。因此,前面显示的查询 检索“words”表的_ID列,即使是 ListView不显示它。这种限制也解释了为什么最多 提供者为每个表都有一个_ID列。