android java.lang.IllegalStateException:无法从CursorWindow读取第0行,第0列

时间:2017-09-30 06:25:00

标签: java android sqlite android-sqlite

我有一个数据库。它工作正常,直到我添加了另一列。而现在它只是不断崩溃与错误消息。

我确保列名称和所有内容都相同。但我仍然收到错误消息。

这是我的DB类

public class DatabaseHelper extends SQLiteOpenHelper {

  public static final String TABLE_NAME1 = "all_receipts";
  public static final String COL_1 = "image";
  public static final String COL_2 = "currentDate";
  public static final String COL_3 = "supplier";
  public static final String COL_4 = "bookType";
  public static final String COL_5 = "total";
  public static final String COL_6 = "tax";
  public static final String COL_7 = "chartofAccs";
  public static final String COL_8 = "paystatus";
  public static final String COL_9 = "uploadedstatus";
  public static final String COL_10 = "tokenid";
  public static final String COL_11 = "seller";
  public static final String COL_12 = "orgemail";
  public static final String COL_13 = "orgid";
  public static final String COL_14 = "currentaccountid";
  public static final String COL_15 = "ID";
  public static final String COL_16 = "mainImage";

  @Override
  public void onCreate(SQLiteDatabase sqLiteDatabase) {

    sqLiteDatabase.execSQL("create table " + TABLE_NAME1 +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,image BLOB, currentDate TEXT," +
            "supplier TEXT, bookType TEXT,total TEXT,tax TEXT,chartofAccs TEXT,paystatus TEXT,uploadedstatus TEXT,tokenid TEXT" +
            ",seller TEXT,orgemail TEXT,orgid TEXT,currentaccountid TEXT,mainImage BLOB)");

    }

   public boolean insertData(String tablename,byte[] image,String currentDate,String supplier,String bookType,String total,String tax,
                          String chartofAccs,String paystatus,String uploadedstatus,String tokenid,String seller,String orgemail,
                          String orgid,String currentaccountid, byte[] mainImage) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    if (tablename.equalsIgnoreCase("all_receipts")) {
        contentValues.put(COL_1, image);
        contentValues.put(COL_2, currentDate);
        contentValues.put(COL_3, supplier);
        contentValues.put(COL_4, bookType);
        contentValues.put(COL_5, total);
        contentValues.put(COL_6, tax);
        contentValues.put(COL_7, chartofAccs);
        contentValues.put(COL_8, paystatus);
        contentValues.put(COL_9, uploadedstatus);
        contentValues.put(COL_10, tokenid);
        contentValues.put(COL_11, seller);
        contentValues.put(COL_12, orgemail);
        contentValues.put(COL_13, orgid);
        contentValues.put(COL_14, currentaccountid);
        contentValues.put(COL_16, mainImage);
        long result = db.insert(tablename, null, contentValues);
        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }
    return false;
}

public ArrayList<Model.Receipts> convertoArray(String tablename){
    ArrayList<Model.Receipts> receiptsArrayList = new ArrayList<>();
    Model.Receipts receipt;

    SQLiteDatabase database = this.getReadableDatabase();
    String selectQuery = "SELECT * FROM all_receipts";


    if(tablename.equalsIgnoreCase("all_receipts")){

        Cursor cursor = database.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                int indexnumber = cursor.getInt(0);
                Bitmap image = getImage(cursor.getBlob(1));
                String currentDate = cursor.getString(2);
                String supplier = cursor.getString(3);
                String bookType = cursor.getString(4);
                String total = cursor.getString(5);
                String tax = cursor.getString(6);
                String chartofAccs = cursor.getString(7);
                String paystatus = cursor.getString(8);
                String uploadedstatus = cursor.getString(9);
                String tokenid = cursor.getString(10);
                String seller = cursor.getString(11);
                String orgemail = cursor.getString(12);
                String orgid = cursor.getString(13);
                String currentaccountid = cursor.getString(14);
                Bitmap mainImage = getImage(cursor.getBlob(16));
                receipt = new Model.Receipts(image,currentDate,supplier,
                        bookType,total,tax,chartofAccs,paystatus,
                        false,tokenid,seller,orgemail,orgid,currentaccountid,indexnumber,mainImage);

                receiptsArrayList.add(receipt);
            }
            while (cursor.moveToNext());
        }
    }
    return receiptsArrayList;

  }
}

首先我保存收据,然后使用convertoArray课程中的database方法拨打收据。

此行会导致错误 - int indexnumber = cursor.getInt(0);

  

无法开始活动   ComponentInfo {com.biz.bookpals.bookpals / com.biz.bookpals.bookpals_android.ViewReceipts}:   java.lang.IllegalStateException:无法读取第0行,第0列   CursorWindow。确保之前正确初始化了Cursor   从中访问数据。                                                                                  在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)                                                                                  在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)                                                                                  在android.app.ActivityThread.access $ 700(ActivityThread.java:157)                                                                                  在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1289)

0 个答案:

没有答案