应用程序因 java.lang.IllegalArgumentException 而崩溃:“_id”列不存在
App crashes due to java.lang.IllegalArgumentException: column '_id' does not exist
我正在制作一个从数据库填充列表视图的应用程序。但是每当我 运行 它时,由于 java.lang.IllegalArgumentException: column '_id'
不存在,应用程序崩溃。首先,我没有字段 ID。
然后我做了一些谷歌搜索,发现我必须在使用 SimpleCursorAdapter
时添加 _id
。但问题仍然存在..
private static final String TABLE_CLASS_LIST = "classlist";
//class list column names
private static final String KEY_LIST_ID = "_id";
private static final String KEY_SUBJECT_NAME_LIST = "subjectname";
private static final String KEY_CLASS_DIV_LIST = "classdivision";
private static final String CREATE_CLASS_LIST = "CREATE TABLE "
+ TABLE_CLASS_LIST + "(" + KEY_LIST_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_SUBJECT_NAME_LIST
+ " TEXT," + KEY_CLASS_DIV_LIST + " TEXT)";
public void addClassList(ClassList classList) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SUBJECT_NAME_LIST, classList.getSubject());
values.put(KEY_CLASS_DIV_LIST, classList.getClassd());
// values.put(KEY_CREATED_AT, user.getCreated_at());
// insert row
long user_id = db.insert(TABLE_CLASS_LIST, null, values);
}
public Cursor fetchTableList() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCursor = db.query(TABLE_CLASS_LIST, new String[]{KEY_SUBJECT_NAME_LIST,
KEY_CLASS_DIV_LIST},
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
以上是我的 dbhelper class(不是全部)。
还有我的Activity
public class ClassActivity extends ActionBarActivity {
ListView classlist;
String value;
private SimpleCursorAdapter dataAdapter;
DatabaseHelper databaseHelper;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class);
classlist = (ListView) findViewById(R.id.listClass);
databaseHelper = new DatabaseHelper(this);
String[] arrayColumns = new String[]{"classdivision", "subjectname"};
int[] arrayViewIDs = new int[]{R.id.classid, R.id.subject};
cursor = databaseHelper.fetchTableList();
dataAdapter = new SimpleCursorAdapter(
this, R.layout.classlist_single_item,
cursor,
arrayColumns,
arrayViewIDs,
0);
classlist.setAdapter(dataAdapter);
value = getIntent().getExtras().getString("add");
您的查询投影缺少 _id 字段,
改变
Cursor mCursor = db.query(TABLE_CLASS_LIST, new String[] { KEY_SUBJECT_NAME_LIST, KEY_CLASS_DIV_LIST},
null, null, null, null, null);
至
Cursor mCursor = db.query(TABLE_CLASS_LIST, new String[] { KEY_LIST_ID, KEY_SUBJECT_NAME_LIST, KEY_CLASS_DIV_LIST},
null, null, null, null, null);
我正在制作一个从数据库填充列表视图的应用程序。但是每当我 运行 它时,由于 java.lang.IllegalArgumentException: column '_id'
不存在,应用程序崩溃。首先,我没有字段 ID。
然后我做了一些谷歌搜索,发现我必须在使用 SimpleCursorAdapter
时添加 _id
。但问题仍然存在..
private static final String TABLE_CLASS_LIST = "classlist";
//class list column names
private static final String KEY_LIST_ID = "_id";
private static final String KEY_SUBJECT_NAME_LIST = "subjectname";
private static final String KEY_CLASS_DIV_LIST = "classdivision";
private static final String CREATE_CLASS_LIST = "CREATE TABLE "
+ TABLE_CLASS_LIST + "(" + KEY_LIST_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_SUBJECT_NAME_LIST
+ " TEXT," + KEY_CLASS_DIV_LIST + " TEXT)";
public void addClassList(ClassList classList) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SUBJECT_NAME_LIST, classList.getSubject());
values.put(KEY_CLASS_DIV_LIST, classList.getClassd());
// values.put(KEY_CREATED_AT, user.getCreated_at());
// insert row
long user_id = db.insert(TABLE_CLASS_LIST, null, values);
}
public Cursor fetchTableList() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCursor = db.query(TABLE_CLASS_LIST, new String[]{KEY_SUBJECT_NAME_LIST,
KEY_CLASS_DIV_LIST},
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
以上是我的 dbhelper class(不是全部)。
还有我的Activity
public class ClassActivity extends ActionBarActivity {
ListView classlist;
String value;
private SimpleCursorAdapter dataAdapter;
DatabaseHelper databaseHelper;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class);
classlist = (ListView) findViewById(R.id.listClass);
databaseHelper = new DatabaseHelper(this);
String[] arrayColumns = new String[]{"classdivision", "subjectname"};
int[] arrayViewIDs = new int[]{R.id.classid, R.id.subject};
cursor = databaseHelper.fetchTableList();
dataAdapter = new SimpleCursorAdapter(
this, R.layout.classlist_single_item,
cursor,
arrayColumns,
arrayViewIDs,
0);
classlist.setAdapter(dataAdapter);
value = getIntent().getExtras().getString("add");
您的查询投影缺少 _id 字段,
改变
Cursor mCursor = db.query(TABLE_CLASS_LIST, new String[] { KEY_SUBJECT_NAME_LIST, KEY_CLASS_DIV_LIST},
null, null, null, null, null);
至
Cursor mCursor = db.query(TABLE_CLASS_LIST, new String[] { KEY_LIST_ID, KEY_SUBJECT_NAME_LIST, KEY_CLASS_DIV_LIST},
null, null, null, null, null);