2016-02-16 103 views
1
我在恢復數據的問題

檢索數據,它說make sure cursor is initialized correctly before accessing data from it我無法弄清楚我的光標正確初始化的問題,錯誤的SQLite數據庫

我下面的logcat。

 02-16 20:34:49.678 22333-22333/? E/AndroidRuntime: FATAL EXCEPTION: main 
                Process: com.example.computer.mathkiddofinal:second, PID: 22333 
                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.computer.mathkiddofinal/com.example.computer.mathkiddofinal.Database.Record_DB}: 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:2338) 
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
                 at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                 at android.os.Handler.dispatchMessage(Handler.java:110) 
                 at android.os.Looper.loop(Looper.java:193) 
                 at android.app.ActivityThread.main(ActivityThread.java:5292) 
                 at java.lang.reflect.Method.invokeNative(Native Method) 
                 at java.lang.reflect.Method.invoke(Method.java:515) 
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
                 at dalvik.system.NativeStart.main(Native Method) 
                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:507) 
                 at android.database.CursorWindow.getInt(CursorWindow.java:574) 
                 at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:80) 
                 at com.example.computer.mathkiddofinal.Database.Record_DB.onCreate(Record_DB.java:55) 
                 at android.app.Activity.performCreate(Activity.java:5264) 
                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302) 
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)  
                 at android.app.ActivityThread.access$800(ActivityThread.java:151)  
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)  
                 at android.os.Handler.dispatchMessage(Handler.java:110)  
                 at android.os.Looper.loop(Looper.java:193)  
                 at android.app.ActivityThread.main(ActivityThread.java:5292)  
                 at java.lang.reflect.Method.invokeNative(Native Method)  

Record_DB.java,在這一行int postwh = res.getInt(res.getColumnIndex("POSTWH"));

public class Record_DB extends Activity { 
Final_Result_Grade_4 asd; 
TextView txtDB; 
    DatabaseHelper myDb; 
    ArrayList<String> results = new ArrayList<String>(); 
    ArrayList<Integer> lpostwh = new ArrayList<Integer>(); 
    ArrayList<Integer> lpostmul = new ArrayList<Integer>(); 
    ArrayList<Integer> lpostdiv = new ArrayList<Integer>(); 
    ArrayList<Integer> lprewh = new ArrayList<Integer>(); 
    ArrayList<Integer> lpremul = new ArrayList<Integer>(); 
    ArrayList<Integer> lprediv = new ArrayList<Integer>(); 
    ListView errorListview; 
    ArrayAdapter<String> adapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_record__db); 
     errorListview = (ListView) findViewById(R.id.listtt); 
     //TextView txtDB = (TextView) findViewById(R.id.txtDB); 
     // txtDB.setMovementMethod(new ScrollingMovementMethod()); 
     myDb = new DatabaseHelper(this); 
     Cursor res = myDb.getAllData(); 
     int cols = res.getColumnCount(); 
     if (res.getCount() == 0) { 
      Toast.makeText(Record_DB.this, "no data", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     while (res.moveToNext()) { 
      int id = res.getInt(res.getColumnIndex("ID")); 
      String name = res.getString(res.getColumnIndex("NAME")); 
      int pre = res.getInt(res.getColumnIndex("PRETEST")); 
      int post = res.getInt(res.getColumnIndex("POSTTEST")); 
      int postwh = res.getInt(res.getColumnIndex("POSTWH")); 
      int postmul = res.getInt(res.getColumnIndex("POSTMUL")); 
      int postdiv = res.getInt(res.getColumnIndex("POSTDIV")); 
      int prewh = res.getInt(res.getColumnIndex("PREWH")); 
      int premul = res.getInt(res.getColumnIndex("PREMUL")); 
      int prediv = res.getInt(res.getColumnIndex("PREDIV")); 
      results.add(""+postwh + postmul + postdiv + prewh + premul + prediv); 


     } 

     adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, results); 
     errorListview.setAdapter(adapter); 

我真的不能找出如何解決這個問題的錯誤,任何人都可以提供幫助。真的很感謝感謝

DataBaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper { 

    public static final String DATABASE_NAME = "gradefour.db"; 
    public static final String TABLE_NAME = "grade_four_table"; 
    public static final String COL_1= "ID"; 
    public static final String COL_2= "NAME"; 
    public static final String COL_3= "PRETEST"; 
    public static final String COL_4= "POSTTEST"; 
    public static final String COL_5= "POSTWH"; 
    public static final String COL_6= "POSTMUL"; 
    public static final String COL_7= "POSTDIV"; 
    public static final String COL_8= "PREWH"; 
    public static final String COL_9= "PREMUL"; 
    public static final String COL_10= "PREDIV"; 

    public DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, 1); 

    } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PRETEST INTEGER,POSTTEST INTEGER,POSTWH INTEGER,POSTMUL INTEGER,POSTDIV INTEGER,PREWH INTEGER,PREMUL INTEGER,PREDIV INTEGER) "); 
    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
    } 
    public boolean inserData(String name, Integer pretest, Integer posttest, Integer postwh, Integer postmul, Integer postdiv, 
          Integer prewh, Integer premul, Integer prediv){ 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(COL_2,name); 
     contentValues.put(COL_3,pretest); 
     contentValues.put(COL_4,posttest); 
     contentValues.put(COL_5,postwh); 
     contentValues.put(COL_6,postmul); 
     contentValues.put(COL_7,postdiv); 
     contentValues.put(COL_8,prewh); 
     contentValues.put(COL_9,premul); 
     contentValues.put(COL_10,prediv); 
     long result = db.insert(TABLE_NAME,null,contentValues); 
     if(result==-1){ 
      return false; 
     } 
     else 
     { 
      return true; 
     } 
    } 
    public Cursor getAllData(){ 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor res = db.rawQuery("select * from "+TABLE_NAME,null); 

     return res; 
    } 
} 
+0

請從您的數據庫輔助類提供代碼.... – Opiatefuchs

+0

@Lynerd:PLZ發表您的數據庫創建表查詢 – kevz

+0

你在光標獲取數據? –

回答

1

按你的意見..!

您有在安裝您的應用程序後添加6列。所以你必須需要重新安裝應用程序,然後創建新表和6列將被添加到您的表中。安裝應用程序時

簡單的只叫你的onCreate()方法,並在那個時候創建​​你的表,所以當你有一些改變,那麼你有兩個選擇

  1. 請在OnUpgrade變化()方法並更新您的應用程序。 (對於實時應用程序)

  2. 卸載應用程序並再次安裝它。 (做測試)

+0

好吧,先生,我會嘗試你的建議 – Dre

+0

謝謝你,你是一個拯救生命的人! :D – Dre

+1

保持它的男人..祝你快樂編碼.. !!! – Nils

1

閱讀光標之前,請

res.moveToFirst(); 
+0

我是否需要在if語句中包含我的while循環if(res.moveToFirst())' – Dre

+0

您不需要在if語句中包含res.moveToFirst(),在while循環之前執行res.moveToFirst()例如,在你創建 – Spirrow

1

嘗試迭代你的遊標,就像下面的例子,不要忘記關閉你的數據庫以及你的遊標對象。

if(cursor.moveToFirst()){ 
    ... 
} 
db.close(); 
cursor.close();