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

android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0游标

岳佐
2023-03-14

我得到以下错误:

android.database.CursorIndexOutOfBoundsException:请求索引0,大小为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(位置);

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

    }
}

共有2个答案

万俟超
2023-03-14

我认为这里有不止一个问题。第一个问题是游标中没有任何行。第二个问题如下。

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

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

if语句周围没有大括号。因此,在游标没有行的情况下,它仍在执行lik Lirik=new Lirik(cursor.getString(1),cursor.getString(2))

请确保您的数据库包含您需要的信息。

翟迪
2023-03-14

您的代码中有几个问题。首先,您需要调用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

不管怎么说,您将保留开放的资源。您从不关闭光标,如果出现任何问题,db也不会关闭。我建议用以下方式重写getlik()函数:

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.on创建()中,您应该检查它以避免进一步的错误:

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匹配的记录(可能您的数据库是空的?)

 类似资料: