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



在我的应用程序中,我有一个表单。哪个用户把CNIC放在第一位。如果他的CNIC已经存在于SQLite数据库中。用户名和手机号码将自动填写在编辑文本中(使用文本观察器)。但是每当我输入相同的CNIC时,应用程序就会崩溃。如果数据库中存在 CNIC,则应用程序不会自动填充名称和电话字段,而是崩溃。我已经尝试了很多,但无法解决我的问题。

查询方式:

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

日志猫错误:

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

编辑:

使用后始终关闭光标。在您的代码中,在 while 循环之后将此行放在方法return listOfSGModel之前getGeneratedData

cursor.close();

相关内容

  • 没有找到相关文章

最新更新