gpt4 book ai didi

android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0

转载 作者:太空宇宙 更新时间:2023-11-03 10:18:37 29 4
gpt4 key购买 nike

我收到以下错误:

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0.

来自这段代码:

  public Lirik getLirik(String id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_MUPUH,
new String[] { KEY_ID, JUDUL, KEY_MUPUH },
KEY_ID + "=?",
new String[] {
String.valueOf(id)
},
null,
null,
null);
if (cursor != null && cursor.moveToFirst())
cursor.moveToFirst();

Lirik lirik = new Lirik(cursor.getString(1),
cursor.getString(2));
db.close();
return lirik;
}

和这个类 ViewMupuh :错误:Lirik lirik = db.getLirik(position);

public class ViewMupuh extends Activity implements OnClickListener {
private static String position = null;

private TextView textJudul, textLirik;
private Button bUpdate;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_mupuh);
Intent intent = getIntent();
position = intent.getStringExtra("position");
if (position != null) {
Log.d("value of position", position);
}
DBHelper db = new DBHelper(this);
Lirik lirik = db.getLirik(position);
textJudul = (TextView) findViewById(R.id.judul_details);
textJudul.setText(lirik.getJudul());
textLirik = (TextView) findViewById(R.id.lirikdetails);
textLirik.setText(lirik.getLirik());

bUpdate = (Button) findViewById(R.id.bupdatedetails);
bUpdate.setOnClickListener(this);
}

@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), EditLirik.class);
intent.putExtra("position value", position);
startActivity(intent);

}
}

最佳答案

您的代码中有几个问题。首先,您不必要地调用了两次 cursor.moveToFirst():

if (cursor != null && cursor.moveToFirst()) // <-- First call
cursor.moveToFirst(); // <-- Second call

此外,即使游标为空或未检索到任何数据,您仍试图从游标获取数据:

if (cursor != null && cursor.moveToFirst())
cursor.moveToFirst();

Lirik lirik = new Lirik(cursor.getString(1),
cursor.getString(2)); // <-- cursor can be null here

无论如何,您要让资源保持打开状态。您永远不会关闭 cursor,如果有任何问题,db 也不会关闭。我建议以这种方式重写您的 getLirik() 函数:

public Lirik getLirik(final String id) {
Lirik lirik = null;

SQLiteDatabase db = null;
Cursor cursor = null;

try {
db = this.getReadableDatabase();
cursor = db.query(TABLE_MUPUH, new String[] { KEY_ID, JUDUL,
KEY_MUPUH }, KEY_ID + "=?", new String[] { id }, null,
null, null);
if (cursor != null && cursor.moveToFirst()) {
lirik = new Lirik(cursor.getString(1), cursor.getString(2));
}
} catch (final Exception e) {
// Do something with the Exception (Log, raise, ...)
} finally {
cursor.close();
db.close();
}

return lirik;
}

如果你这样做,请考虑到你在调用 getLirik() 时可能会得到 null,因此,你应该在你的 ViewMupuh.onCreate() 中检查它避免进一步的错误:

textJudul = (TextView) findViewById(R.id.judul_details);
textLirik = (TextView) findViewById(R.id.lirikdetails);

DBHelper db = new DBHelper(this);
Lirik lirik = db.getLirik(position);

if (lirik != null) {
textJudul.setText(lirik.getJudul());
textLirik.setText(lirik.getLirik());
}

还要考虑到所有提示都指向您的数据库中没有与您查询的 ID 相匹配的记录(也许您的数据库是空的?)

关于android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30175889/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com