问题描述
每当我启动我的应用程序时,我的 LogCat 中都会出现 java.lang.IllegalArgumentException: column '_id' does not exist
错误.我创建了列 '_id'
,但它仍然抛出这个.这是我的主要 .java:
package com.gantt.shoppinglist;导入android.app.Dialog;导入android.app.ListActivity;导入android.database.Cursor;导入android.os.Bundle;导入android.view.View;导入 android.view.View.OnClickListener;导入android.widget.Button;导入 android.widget.EditText;导入 android.widget.ListView;导入 android.widget.SimpleCursorAdapter;公共类 ShoppingList 扩展 ListActivity {私有数据助手数据助手;/** 在第一次创建活动时调用.*/@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);设置内容视图(R.layout.main);DataHelper = new DataHelper(this);光标 c = (Cursor) DataHelper.selectAll();long id = c.getLong(c.getColumnIndex(_id"));开始管理光标(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 适配器 = 新的 SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,c,从,到);lv.setAdapter(适配器);按钮 button1main = (Button) findViewById(R.id.add);button1main.setOnClickListener(new OnClickListener() {@覆盖公共无效 onClick(查看 v){最终对话框 additem = new Dialog(ShoppingList.this);additem.setContentView(R.layout.maindialog);最后的 EditText et = (EditText)additem.findViewById(R.id.edittext);additem.setTitle("输入你的项目");additem.setCancelable(true);et.setHint("键入一个项目的名称...");按钮按钮 = (Button) additem.findViewById(R.id.cancel);button.setOnClickListener(new OnClickListener() {@覆盖公共无效 onClick(查看 v){additem.dismiss();}});additem.show();按钮 ok = (Button) additem.findViewById(R.id.ok);ok.setOnClickListener(new OnClickListener() {@覆盖公共无效 onClick(查看 v){最终字符串文本 = et.getText().toString();additem.dismiss();et.setText("");}});}});}}
这是我的 DataHelper 类:
package com.gantt.shoppinglist;导入 java.util.ArrayList;导入 java.util.List;导入android.content.Context;导入android.database.Cursor;导入android.database.sqlite.SQLiteDatabase;导入 android.database.sqlite.SQLiteOpenHelper;导入 android.database.sqlite.SQLiteStatement;导入android.util.Log;公共类 DataHelper {private static final String DATABASE_NAME = "items.db";私有静态最终 int DATABASE_VERSION = 1;private static final String TABLE_NAME = "table1";公共静态最终字符串 KEY_ROWID = "_id";私有上下文上下文;私有 SQLiteDatabase 数据库;私有 SQLiteStatement insertStmt;private static final String INSERT = "插入"+ TABLE_NAME +(名称)值(?)";公共数据助手(上下文上下文){this.context = 上下文;OpenHelper openHelper = new OpenHelper(this.context);this.db = openHelper.getWritableDatabase();this.insertStmt = this.db.compileStatement(INSERT);}公共长插入(字符串名称){this.insertStmt.bindString(1, name);返回 this.insertStmt.executeInsert();}公共无效删除所有(){this.db.delete(TABLE_NAME, null, null);}公共游标 selectAll() {列表<字符串>list = new ArrayList<String>();光标 cursor = this.db.query(TABLE_NAME, new String[] { "name" },空,空,空,空,名称描述");if (cursor.moveToFirst()) {做 {list.add(cursor.getString(0));} while (cursor.moveToNext());}if (cursor != null && !cursor.isClosed()) {光标.close();}返回光标;}公共静态字符串 getDatabaseName() {返回 DATABASE_NAME;}私有静态类 OpenHelper 扩展 SQLiteOpenHelper {OpenHelper(上下文上下文){超级(上下文,getDatabaseName(),空,DATABASE_VERSION);}@覆盖公共无效onCreate(SQLiteDatabase db){db.execSQL("CREATE TABLE "+ TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT");}@覆盖公共无效 onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){Log.w("Example", "Upgradeing database, this will drop tables and recreate.");db.execSQL("如果存在则删除表" + TABLE_NAME);onCreate(db);}}}
我有一个类似的问题 - 我认为这是一个必须选择"(或选择为")名为 _id
因为 SimpleCursorAdapter
需要它.
来自文档:p>
处理内容 URI ID
按照惯例,提供程序通过以下方式提供对表中单行的访问权限接受带有 ID 值的内容 URIURI.同样按照惯例,提供者将 ID 值与表的值匹配_ID
列,并针对匹配的行执行请求的访问.
此约定有助于应用访问的通用设计模式提供者.该应用程序对提供者进行查询并显示使用 CursorAdapter
在 ListView
中生成 Cursor
.定义CursorAdapter
要求 Cursor
中的列之一为 _ID
.
就我而言,我的表中有一个名为oid"的自动编号列,因此我将 SELECT 命令更改为(例如)...
SELECT oid as _id, name, number FROM mytable
这解决了我的问题.
编辑以显示更广泛的代码...
@Override公共无效 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);}公共 SimpleCursorAdapter getGuideAdapter() {SimpleCursorAdapter 适配器 = 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 };适配器 = 新的 SimpleCursorAdapter(this, R.layout.channel_selector_item, cursor, columnNames, resIds);}返回适配器;}
Whenever I start my app, I get a java.lang.IllegalArgumentException: column '_id' does not exist
error in my LogCat. I have created the column '_id'
, but it still throws this. Here is my main .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("");
}
});
}
});
}
}
Here is my DataHelper class:
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);
}
}
}
I had a similar issue - I think it's a case of having to 'select' (or 'select as') something called _id
because the SimpleCursorAdapter
needs it.
From the documentation:
In my case, I have an autonumber column in my table called 'oid' so I altered my SELECT command to be (for example)...
SELECT oid as _id, name, number FROM mytable
This cured the problem for me.
EDIT to show more extensive code...
@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;
}
这篇关于由于 java.lang.IllegalArgumentException,应用程序在启动时崩溃:列“_id"不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!