简体   繁体   中英

Android: Make sure the Cursor is initialized correctly before accessing data from it

I am trying to build an Android todo list app that uses SQL database to save user input(title,task,date,time). In the project I use 2 activities (MainActivity, Dialog), the code I'm using successfully saves the user input using an onClick(Add_task), but when I use more than one Cursor to update the listview UI it gives the following error.

Logcat

08-08 13:42:15.540 6051-6051/com.naive.LISTY E/AndroidRuntime: FATAL EXCEPTION: main
                                                           Process: com.naive.LISTY, PID: 6051
                                                           java.lang.IllegalStateException: Could not execute method for android:onClick
                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:289)
                                                               at android.view.View.performClick(View.java:5198)
                                                               at android.view.View$PerformClick.run(View.java:21147)
                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                               at android.os.Looper.loop(Looper.java:148)
                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                            Caused by: java.lang.reflect.InvocationTargetException
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                               at android.view.View.performClick(View.java:5198) 
                                                               at android.view.View$PerformClick.run(View.java:21147) 
                                                               at android.os.Handler.handleCallback(Handler.java:739) 
                                                               at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                               at android.os.Looper.loop(Looper.java:148) 
                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                            Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                               at android.database.CursorWindow.nativeGetString(Native Method)
                                                               at android.database.CursorWindow.getString(CursorWindow.java:438)
                                                               at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
                                                               at listy.naive.com.listy.MainActivity.updateUI(MainActivity.java:102)
                                                               at listy.naive.com.listy.Dialog.Add_task(Dialog.java:58)
                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
                                                               at android.view.View.performClick(View.java:5198) 
                                                               at android.view.View$PerformClick.run(View.java:21147) 
                                                               at android.os.Handler.handleCallback(Handler.java:739) 
                                                               at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                               at android.os.Looper.loop(Looper.java:148) 
                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

updateUI

Update UI

public void updateUI() {
    ArrayList<String> taskList = new ArrayList<>();
    SQLiteDatabase db = mHelper.getReadableDatabase();

    Cursor title_cursor = db.query(TaskContract.TaskEntry.TABLE,                                // Cursor Title
            new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
            null, null, null, null, null);

    Cursor time_cursor = db.query(TaskContract.TaskEntry.TABLE,                                // Cursor Title
            new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TIME},
            null, null, null, null, null);

    while (title_cursor.moveToNext()) {
        int idx = title_cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
        taskList.add(title_cursor.getString(idx));
    }

    if (mAdapter == null) {
        mAdapter = new ArrayAdapter<>(this,
                R.layout.item_todo,
                R.id.task_title,
                taskList);
        mTaskListView.setAdapter(mAdapter);
    } else {
        mAdapter.clear();
        mAdapter.addAll(taskList);
        mAdapter.notifyDataSetChanged();
    }


    while (time_cursor.moveToNext()) {
        int idx = time_cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
        taskList.add(time_cursor.getString(idx));
    }

    if (mAdapter == null) {
        mAdapter = new ArrayAdapter<>(this,
                R.layout.item_todo,
                R.id.task_time,
                taskList);
        mTaskListView.setAdapter(mAdapter);
    } else {
        mAdapter.clear();
        mAdapter.addAll(taskList);
        mAdapter.notifyDataSetChanged();
    }

    time_cursor.close();
    title_cursor.close();
    db.close();
}

Try moving cursor to first row before accessing it:

Cursor time_cursor = db.query(TaskContract.TaskEntry.TABLE,                                // Cursor Title
        new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TIME},
        null, null, null, null, null);
  if(time_cursor != null){
      time_cursor.moveToFirst(); // this is part of cursor initialization

      while (title_cursor.moveToNext()) {
         int idx = title_cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
         taskList.add(title_cursor.getString(idx));
      }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM