IllegalArgumentException: column '_id' does not exist when call to SimpleCursorAdaptor
I have a table named "master" with columns id
, name
, surname
, gender
, and designation
When I fire off a query to get a Cursor object for a CursorAdapter I get:
IllegalArgumentException: column '_id' does not exist when call to CursorAdaptor
But I don't have a columen named "_id".
Can anybody tell me why I am getting this error?
Here is the stack trace:
07-13 15:45:40.582: WARN/System.err(295): java.lang.IllegalArgumentException: column '_id' does not exist
07-13 15:45:40.592: WARN/System.err(295): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
07-13 15:45:40.592: WARN/System.err(295): at android.widget.CursorAdapter.changeCursor(CursorAdapter.java:257)
07-13 15:45:40.602: WARN/System.err(295): at com.praumtech.names4baby.ui.NamesListAdapter.setCursor(NamesListAdapter.java:63)
07-13 15:45:40.602: WARN/System.err(295): at com.praumtech.names4baby.ui.BoysNamesListActivity.initNameList(BoysNamesListActivity.java:79)
07-13 15:45:40.602: WARN/System.err(295): at com.praumtech.names4baby.ui.BoysNamesListActivity.onCreate(BoysNamesListActivity.java:49)
0开发者_运维技巧7-13 15:45:40.602: WARN/System.err(295): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
07-13 15:45:40.602: WARN/System.err(295): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
07-13 15:45:40.612: WARN/System.err(295): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
07-13 15:45:40.612: WARN/System.err(295): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
07-13 15:45:40.612: WARN/System.err(295): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
07-13 15:45:40.612: WARN/System.err(295): at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 15:45:40.621: WARN/System.err(295): at android.os.Looper.loop(Looper.java:123)
07-13 15:45:40.621: WARN/System.err(295): at android.app.ActivityThread.main(ActivityThread.java:4203)
07-13 15:45:40.621: WARN/System.err(295): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 15:45:40.621: WARN/System.err(295): at java.lang.reflect.Method.invoke(Method.java:521)
07-13 15:45:40.621: WARN/System.err(295): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-13 15:45:40.621: WARN/System.err(295): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
07-13 15:45:40.631: WARN/System.err(295): at dalvik.system.NativeStart.main(Native Method)
This is happening because CursorAdapter
must have an _id
column in the table it is using.
With the Database(SQLite) in Android applications, it is better to add a column named _id" to all tables in order to be able to use CursorAdapter
.
Alternatively, you can write an sql statement like
select member_id as _id from member _table where ....."
in order to get a Cursor
which may be used with CursorAdapter
.
This is specified in CursorAdaptor
's documentation:
Adapter that exposes data from a Cursor to a ListView widget. The Cursor must include a column named "_id" or this class will not work."
Your namemaster
tables needs to have a column _ID
defined to be used by SimpleCursorAdaptor. Make sure your table schema includes _ID
and not an id
as the later is wrong.
Simple and Salty
To overcome this problem, you have to rename the primary key field name as _id
.
I believe the SimpleCursorAdapter assumes that there is a column '_id'. You need to create it so it would probably be easier for you to use "_id" instead of your own "id". Read the 2nd post on this page
精彩评论