Android studio 使用SimpleCursorAdapter从SQL DB填充ListView时出错

Android studio 使用SimpleCursorAdapter从SQL DB填充ListView时出错,android-studio,android-sqlite,simplecursoradapter,Android Studio,Android Sqlite,Simplecursoradapter,所以,我试着写一个程序,在那里我可以创建任务,在listview中显示它们的名称和日期,然后在listview中单击它们后简单地编辑它们 我在人口方面有一些问题。我需要使用SimpleCrsorAdapter作为我大学的作业来完成这个项目 这是我的填充函数,它位于我的MainActivity中 private void populate(){ Cursor cursor = myDb.getAllRows(); String[] backDB = new String[] {D

所以,我试着写一个程序,在那里我可以创建任务,在listview中显示它们的名称和日期,然后在listview中单击它们后简单地编辑它们

我在人口方面有一些问题。我需要使用SimpleCrsorAdapter作为我大学的作业来完成这个项目

这是我的填充函数,它位于我的MainActivity中

 private void populate(){
    Cursor cursor = myDb.getAllRows();
    String[] backDB = new String[] {DBAdapter.COLUMN_NAME, DBAdapter.COLUMN_DATE};
    int[] toView = new int[] {R.id.textViewName, R.id.textViewDate};
    SimpleCursorAdapter myCursor;
    myCursor = new SimpleCursorAdapter(getBaseContext(), R.layout.row_layout, cursor, backDB,toView, 0);
    ListView myList = (ListView) findViewById(R.id.listViewTasks);
    myList.setAdapter(myCursor);
}
我认为这可能与sql数据库有关,尤其是getAllRows函数,因为在logcat中,我可以看到行中有一个问题:

 myCursor = new SimpleCursorAdapter(getBaseContext(), R.layout.row_layout, cursor, backDB,toView, 0); 
这是我的getAllRows函数

 public Cursor getAllRows() {
    String query = "SELECT * FROM " + TABLE_NAME;
    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();
    return c;
}
顺便说一句,我的程序刚刚崩溃并一直崩溃。

您的问题可能是因为您没有名为_id的列,在这种情况下,日志将包含以下内容:-

06-07 10:53:53.957 1178-1178/so50635292.so50635292 E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{so50635292.so50635292/so50635292.so50635292.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        at android.app.ActivityThread.access$600(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
        at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:302)
        at android.widget.CursorAdapter.init(CursorAdapter.java:168)
        at android.widget.CursorAdapter.<init>(CursorAdapter.java:145)
        at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
        at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
        at so50635292.so50635292.MainActivity.populate(MainActivity.java:31)
        at so50635292.so50635292.MainActivity.onCreate(MainActivity.java:23)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
        at android.app.ActivityThread.access$600(ActivityThread.java:130) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:4745) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
        at dalvik.system.NativeStart.main(Native Method) 

注意:以下假设您没有使用ROWID进行编码
请从日志中添加堆栈跟踪。
 String query = "SELECT * FROM " + TABLE_NAME;
String query = "SELECT rowid AS _id,* FROM " + TABLE_NAME;