android.database.CursorIndexOutOfBoundsException: 请求索引 0,游标大小为 0
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 cursor
我收到以下错误:
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;
}
和这个 class 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);
}
}
我认为这里的问题不止一个。第一个问题是游标中没有任何行。第二个问题如下。
if (cursor != null && cursor.moveToFirst())
cursor.moveToFirst();
Lirik lirik = new Lirik(cursor.getString(1),
cursor.getString(2));
db.close();
return lirik;
if 语句周围没有大括号。结果在游标没有行的情况下,还是在执行Lirik lirik = new Lirik(cursor.getString(1),
cursor.getString(2));
请确保您的数据库中有您需要的信息。
您的代码中有几个问题。首先,您不必要地调用了 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, 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;
}
和这个 class 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);
}
}
我认为这里的问题不止一个。第一个问题是游标中没有任何行。第二个问题如下。
if (cursor != null && cursor.moveToFirst())
cursor.moveToFirst();
Lirik lirik = new Lirik(cursor.getString(1),
cursor.getString(2));
db.close();
return lirik;
if 语句周围没有大括号。结果在游标没有行的情况下,还是在执行Lirik lirik = new Lirik(cursor.getString(1),
cursor.getString(2));
请确保您的数据库中有您需要的信息。
您的代码中有几个问题。首先,您不必要地调用了 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 相匹配的记录(也许您的数据库是空的?)