2017-04-30 18 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

あなたはどこに問題があるのか​​分からないのですか? '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」列名がチェックのように行われる一方、 1つはスキーマごとです。

+0

これはうまくいきました。はい、COL_2が間違いでした、ありがとうございます。 –

+0

それが助けて嬉しいことを歓迎:) – Pavan

関連する問題