2017-04-30 10 views
-1

내 응용 프로그램에서 작동하는 그래프를 가져 오는 데 문제가 있습니다. 그러나이 코드에서 어디서 잘못 될지 알 수 없습니다. 코드가 만들어 지지만 활동을 선택하면 작동이 중단됩니다. 데이터베이스에서 데이터를 얻기 전에 그래프를 만들려고 시도했기 때문에 잘못 구성했을 수도 있습니다. 누군가 내 실수를 지적 할 수 있다면, 나는 감사 할 것입니다.CursorWindow에서 행 0, col -1을 읽을 수 없습니다.

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)  
+0

무엇이 문제인지 알 수 없습니다. 'at com.example.fitmess.project.GraphActivity.onCreate (GraphActivity.java:36)' –

답변

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()); 
} 

은 "col_2에는"열 이름이 확인처럼 수행됩니다 동안 스키마 당 하나씩

+0

환호성입니다. 그렇습니다. 예 COL_2가 실수였습니다. 감사합니다. –

+0

도움이 된 것을 기쁜 마음으로 환영합니다. :) – Pavan