![](/img/trans.png)
[英]App crashes due to java.lang.IllegalArgumentException: column '_id' does not exist
[英]App Crashes On Startup Due To java.lang.IllegalArgumentException: column '_id' does not exist
每當我啟動應用程序時,都會收到java.lang.IllegalArgumentException: column '_id' does not exist
LogCat中的java.lang.IllegalArgumentException: column '_id' does not exist
錯誤。 我創建了列'_id'
,但是它仍然拋出該列。 這是我的主要.java:
package com.gantt.shoppinglist;
import android.app.Dialog;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class ShoppingList extends ListActivity {
private DataHelper DataHelper;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DataHelper = new DataHelper(this);
Cursor c = (Cursor) DataHelper.selectAll();
long id = c.getLong(c.getColumnIndex("_id"));
startManagingCursor(c);
ListView lv = (ListView) findViewById(android.R.id.list);
String[] from = new String[] { com.gantt.shoppinglist.DataHelper.getDatabaseName() };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, c, from, to);
lv.setAdapter(adapter);
Button button1main = (Button) findViewById(R.id.add);
button1main.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Dialog additem = new Dialog(ShoppingList.this);
additem.setContentView(R.layout.maindialog);
final EditText et = (EditText)additem.findViewById(R.id.edittext);
additem.setTitle("Type your item");
additem.setCancelable(true);
et.setHint("Type the name of an item...");
Button button = (Button) additem.findViewById(R.id.cancel);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
additem.dismiss();
}
});
additem.show();
Button ok = (Button) additem.findViewById(R.id.ok);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String text = et.getText().toString();
additem.dismiss();
et.setText("");
}
});
}
});
}
}
這是我的DataHelper類:
package com.gantt.shoppinglist;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
public class DataHelper {
private static final String DATABASE_NAME = "items.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "table1";
public static final String KEY_ROWID = "_id";
private Context context;
private SQLiteDatabase db;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into "
+ TABLE_NAME + "(name) values (?)";
public DataHelper(Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
this.insertStmt = this.db.compileStatement(INSERT);
}
public long insert(String name) {
this.insertStmt.bindString(1, name);
return this.insertStmt.executeInsert();
}
public void deleteAll() {
this.db.delete(TABLE_NAME, null, null);
}
public Cursor selectAll() {
List<String> list = new ArrayList<String>();
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" },
null, null, null, null, "name desc");
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return cursor;
}
public static String getDatabaseName() {
return DATABASE_NAME;
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, getDatabaseName(), null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Example", "Upgrading database, this will drop tables and recreate.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
我有一個類似的問題-我認為這是必須“選擇”(或“選擇為”)名為_id
因為SimpleCursorAdapter
需要它。
從文檔中 :
處理內容URI ID
按照約定,提供程序通過接受內容URI(在URI末尾具有該行的ID值)來提供對表中單個行的訪問。 同樣,按照慣例,提供程序將ID值與表的
_ID
列匹配,並對匹配的行執行請求的訪問。此約定為訪問提供程序的應用程序提供了一種通用的設計模式。 該應用程序對提供程序進行查詢,並使用
CursorAdapter
在ListView
顯示結果Cursor
。CursorAdapter
的定義要求Cursor
中的列之一為_ID
。
就我而言,我的表中有一個名為“ oid”的自動編號列,因此我將SELECT命令更改為(例如)...
SELECT oid as _id, name, number FROM mytable
這為我解決了問題。
編輯以顯示更廣泛的代碼...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.channel_selector);
GridView channel_selector_grid = (GridView) findViewById(R.id.channel_grid);
sca = getGuideAdapter();
channel_selector_grid.setAdapter(sca);
}
public SimpleCursorAdapter getGuideAdapter() {
SimpleCursorAdapter adapter = null;
SQLiteDatabase db = SQLiteDatabaseHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT DISTINCT oid as _id, name, number FROM CHAN_TABLE ORDER BY number", null);
if (cursor.moveToFirst()) {
String[] columnNames = { "name" };
int[] resIds = { R.id.channel_name };
adapter = new SimpleCursorAdapter(this, R.layout.channel_selector_item, cursor, columnNames, resIds);
}
return adapter;
}
嗯,不確定您是否將其粘貼錯了,但是:
DataHelper = new DataHelper(this);
Cursor c = (Cursor) DataHelper.selectAll();
是錯的。 您需要將其聲明為一個對象,並在已初始化的DataBase對象上調用您的方法:
DataHelper dataHelper = new DataHelper(this);
Cursor c = (Cursor) dataHelper.selectAll();
糟糕,您剛剛用大寫字母D聲明了變量名,不建議這樣做,也不建議使用標准的Java編碼樣式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.