由于java.lang.IllegalArgumentException,应用程序因启动而崩溃:列’_id’不存在

每当我启动我的应用程序时,我都会得到一个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 list = new ArrayList(); 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); } } } 

  • 方法调用可能会产生java NullpointerException
  • 无法恢复活动错误
  • 分配游标时内存不足
  • SimpleCursorAdapter,包含Android中的部分
  • 无法分配CursorWindow
  • 使用SimpleCursorAdapter从Cursor更改值
  • 如何检查游标是否为空?
  • 手动将行添加到Cursor中
  • 我有一个类似的问题 – 我认为这是一个必须’选择’(或’选择’)称为_id事情,因为SimpleCursorAdapter需要它。

    从文档 :

    处理内容URI ID

    按照惯例,提供程序通过接受URI末尾的行ID值的内容URI来提供对表中单行的访问。 此外,按照惯例,提供程序将ID值与表的_ID列匹配,并对匹配的行执行请求的访问。

    此约定有助于访问提供者的应用程序的通用设计模式。 该应用程序对提供程序执行查询,并使用CursorAdapterListView显示生成的CursorCursorAdapter的定义要求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声明了你的variables名,这不是推荐的,也不是标准的java编码风格。

    http://java.about.com/od/javasyntax/a/nameconventions.htm