android中的非法参数异常:列_id不存在

时间:2012-06-19 18:38:03

标签: android exception-handling sqlite android-listview android-cursor

这是我尝试运行应用程序时遇到的错误

android中的非法参数异常:列_id不存在

   public static final String OBJ_ID="_id";

并且数据库的创建如下:

 private static final String CREATE_TAB1= "create table " + TABLE_1 + " (" + OBJ_ID + " integer primary key                                     
    autoincrement, " + OBJ_NAME + " text not null, " + DESC1 + " text not null, " + DESC2 + " text not null);"; 

尝试使用databasehandler中的以下函数检索数据:

     public Cursor getAllTable1() {
                return db.query(TABLE_1, 
                                new String[] { OBJ_NAME }, 
                                null, null, null, null, null);
              }

这是我与数据库

通信的功能
    public class List_View extends ListActivity {



public void onCreate(Bundle icicle)
{
    super.onCreate(icicle); 
    setContentView(R.layout.displayitems); 

     Databasehelp db = new Databasehelp(this);
    db.open();


    Cursor cursor = db.getAllTable1();

     startManagingCursor(cursor); 

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
           R.layout.list_example_entry, cursor, 
           new String[] {"name"}, 
           new int[] {R.id.name_entry }); 
    setListAdapter(adapter);
}
}

虽然我的数据库架构由名为_id的列组成。我把它改成了甚至_ID但似乎没有用。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

绑定到ListView(Spinner等)时,必须选择列_id

所以像这样添加_id列:

public Cursor getAllTable1() {
    return db.query(TABLE_1, 
            new String[] { OBJ_ID, OBJ_NAME }, 
            null, null, null, null, null);
}