当前位置: 首页 > 知识库问答 >
问题:

无法从CursorWindow读取第0行,第2列。在从游标访问数据之前,请确保游标已正确初始化

郤浩慨
2023-03-14
public List<SGenerateModel> getGeneratedData(String seller_cnic){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT " + TABLE_COL2 + ", " + TABLE_COL3 + " FROM " + TABLE_NAME + " WHERE " + TABLE_COL4 + " =?" ;
    Cursor cursor = db.rawQuery(query, new String[]{"" + seller_cnic});

    List<SGenerateModel> listOfSGModel = new ArrayList<>();
    while(cursor.moveToNext()){
        SGenerateModel sGenerateModel = new SGenerateModel(cursor.getString(1), cursor.getString(2));
        sGenerateModel.setId(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TABLE_COL1)));
        listOfSGModel.add(sGenerateModel);
    }
    return listOfSGModel;
}
   public void generate(){

    if (etSname != null)
        selName = (!etSname.getText().toString().equals("")?
                etSname.getText().toString() : "NULL");

    if (etSphone != null)
        selPhone = (!etSphone.getText().toString().equals("")?
                etSphone.getText().toString() : "NULL");

    if (etScnic != null)
        selCnic = (!etScnic.getText().toString().equals("")?
                etScnic.getText().toString() : "NULL");

    sGenerateModelList = databaseHelper.getGeneratedData(selCnic);
    for (SGenerateModel sGenerateModel : sGenerateModelList){
        selName = sGenerateModel.getSellerName() ;
        etSname.setText(selName);
        selPhone = sGenerateModel.getSellerMobile();
        etSphone.setText(selPhone);
    }
}

TextWatcher代码:

TextWatcher textWatcher2 = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            generate();
        }
    };

etSname.addTextChangedListener(textWatcher2);
    etSphone.addTextChangedListener(textWatcher2);
    etScnic.addTextChangedListener(textWatcher2);

LogCat错误:

 java.lang.IllegalStateException: Couldn't read row 0, col 2 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
    at android.database.CursorWindow.nativeGetString(Native Method)
    at android.database.CursorWindow.getString(CursorWindow.java:438)
    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
    at com.example.cattleapp.helpers.DatabaseHelper.getGeneratedData(DatabaseHelper.java:218)
    at com.example.cattleapp.activities.MainActivity.generate(MainActivity.java:536)
    at com.example.cattleapp.activities.MainActivity$7.afterTextChanged(MainActivity.java:241)
    at android.widget.TextView.sendAfterTextChanged(TextView.java:9485)
    at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:12080)
    at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1262)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:574)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:507)
    at com.santalu.maskedittext.MaskTextWatcher.format(MaskTextWatcher.kt:67)
    at com.santalu.maskedittext.MaskTextWatcher.afterTextChanged(MaskTextWatcher.kt:21)
    at android.widget.TextView.sendAfterTextChanged(TextView.java:9485)

共有1个答案

屈翰飞
2023-03-14
String query = "SELECT " + TABLE_COL2 + ", " + TABLE_COL3 + " FROM " + TABLE_NAME + " WHERE " + TABLE_COL4 + " =?" ;

在这一行中,您只获取2列(索引0和1),但是在获取时,您试图获取第2列。即。

cursor.getString(2)

只能获得列0和1。

试试这个:

SGenerateModel sGenerateModel = new SGenerateModel(cursor.getString(0), cursor.getString(1));
cursor.close();
 类似资料: