gpt4 book ai didi

java - 如何修改 Cursor 导致 IllegalArgumentException : column '_id' does not exist

转载 作者:行者123 更新时间:2023-12-02 01:34:18 28 4
gpt4 key购买 nike

当我启动 TestSelected Activity logcat 时显示:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sjkdev.androidsqlite/com.sjkdev.androidsqlite.TestSelected}: java.lang.IllegalArgumentException: column '_id' does not exist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:333)
at android.widget.CursorAdapter.init(CursorAdapter.java:180)
at android.widget.CursorAdapter.<init>(CursorAdapter.java:157)
at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:96)
at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
at com.sjkdev.androidsqlite.TestSelected.onCreate(TestSelected.java:37)
at android.app.Activity.performCreate(Activity.java:6910)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:156) 
at android.app.ActivityThread.main(ActivityThread.java:6523) 
at java.lang.reflect.Method.invoke(Native Method) 

正如您在我的代码中看到的那样,我有名为“_id”的列:

SQLiteAdapter.java

public class SQLiteAdapter {

public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String _id = "_id";
public static final String KEY_NAME = "Name";
public static final String KEY_PRICE = "Price";
public static final String KEY_QUANTITY = "Quantity";
public static final String KEY_MU = "MU";
public static final String KEY_PDATE = "PDate";
public static final String KEY_SHOP = "Shop";

//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ _id + " integer primary key autoincrement, "
+ KEY_NAME + " text, "
+ KEY_PRICE + " text,"
+ KEY_QUANTITY + " text, "
+ KEY_MU + " text, "
+ KEY_PDATE + " text, "
+ KEY_SHOP + " text);";

private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;

private Context context;

public SQLiteAdapter(Context c){
context = c;
}

public SQLiteAdapter openToRead() {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}

public void close(){
sqLiteHelper.close();
}


public Cursor makeSelected(){
String[] selectedColumn = new String[]{KEY_NAME, KEY_PRICE};
Cursor selectedCursor = sqLiteDatabase.query(MYDATABASE_TABLE, selectedColumn,
null, null, null, null, null);
return selectedCursor;
}

public class SQLiteHelper extends SQLiteOpenHelper {

public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}

}

TestSelected.java

public class TestSelected extends AppCompatActivity {

private SQLiteAdapter mySQLiteAdapter;
ListView selectedList;
SimpleCursorAdapter selectedCursorAdapter;
Cursor selectedCursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_selected);

selectedList = (ListView) findViewById(R.id.list_selected);

mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
selectedCursor = mySQLiteAdapter.makeSelected();



selectedCursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
selectedCursor,
new String[]{"KEY_PRICE"},
new int[]{android.R.id.text1}, 0);
selectedList.setAdapter(selectedCursorAdapter);

}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mySQLiteAdapter.close();
}

private void updateList(){
selectedCursor.requery();
}
}

我认为我犯了错误

  1. public SQLiteAdapter openToRead() { ... } 或/与

  2. 公共(public)光标 makeSelected(){ ... } 或/与

  3. 在TestSelected.java中打开并制作光标

最佳答案

传递给 CursorAdapterCursor 需要在其结果集中包含 _id 列。您可以在此处添加它:

public Cursor makeSelected(){
String[] selectedColumn = new String[]{_id, KEY_NAME, KEY_PRICE};
//-------------------------------------^^^
<小时/>

后续:

now logcat shows: > column 'KEY_PRICE' does not exist

列的名称位于常量 KEY_PRICE 中,而不是字符串文字 "KEY_PRICE"。改变

new String[]{"KEY_PRICE"},

new String[]{KEY_PRICE},

关于java - 如何修改 Cursor 导致 IllegalArgumentException : column '_id' does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55473970/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com