无法从CursorWindow读取第0行col -1

时间:2017-04-30 15:47:45

标签: java android android-graphview

我在尝试在我的应用中使用图表时遇到问题,但我不知道这个代码在哪里出错了。代码构建但崩溃选择活动。我想我可能错误地构造了它,因为我可能在从数据库获取数据之前尝试创建图形。如果有人能指出我的错误,我将不胜感激。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_graph);
    myDB = new DatabaseCode(this);
    SQLiteDatabase db = myDB.getWritableDatabase();
    Cursor cursor=db.rawQuery("select * from "+TABLE_NAME,null);
        if(cursor.getCount()==0){
            return;
        }
        else {
            ArrayList<Integer> ListArray = new ArrayList<Integer>();
            //
            while (cursor.moveToNext()) {
                ListArray.add(cursor.getInt(cursor.getColumnIndex("COL_2")));
            }
            GraphView graph = (GraphView) findViewById(R.id.graph);
            LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {

                    new DataPoint(0,ListArray.get(0)),
                    new DataPoint(1,ListArray.get(1)),
                    new DataPoint(2,ListArray.get(2))
            });
            graph.addSeries(series);
        }


}

}

这是错误日志。

04-30 16:34:18.912 20125-20125/com.example.fitmess.project E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.example.fitmess.project, PID: 20125
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fitmess.project/com.example.fitmess.project.GraphActivity}: 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.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
                                                                             at android.app.ActivityThread.access$1100(ActivityThread.java:229)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:7331)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                          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.nativeGetLong(Native Method)
                                                                             at android.database.CursorWindow.getLong(CursorWindow.java:524)
                                                                             at android.database.CursorWindow.getInt(CursorWindow.java:591)
                                                                             at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
                                                                             at com.example.fitmess.project.GraphActivity.onCreate(GraphActivity.java:36)
                                                                             at android.app.Activity.performCreate(Activity.java:6904)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
                                                                             at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:7331) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

1 个答案:

答案 0 :(得分:0)

从光标访问数据时,你必须检查光标中的数据并将光标移动到第一个位置,循环将如下例所示

if(cursor.moveToFirst()) //this will move cursor to first position
{
   do{
//your code to fetch data from cursor
  ListArray.add(cursor.getInt(cursor.getColumnIndex("COL_2")));
    }while(cursor.moveToNext());
}

还要检查&#34; COL_2&#34;根据模式,列名是正确的。