Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android.database.CursorIndexOutOfBoundsException…再次_Java_Android_Sqlite - Fatal编程技术网

Java android.database.CursorIndexOutOfBoundsException…再次

Java android.database.CursorIndexOutOfBoundsException…再次,java,android,sqlite,Java,Android,Sqlite,因此,我的应用程序的用户崩溃报告中有此错误。报告如下: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 at android.database.AbstractCursor.checkPosition(AbstractCursor.) at android.database.AbstractWindowedCursor.checkPosition(AbstractWindo

因此,我的应用程序的用户崩溃报告中有此错误。报告如下:

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at rs.androidaplikacije.zastaveigradovi.Kviz10Hard.nextQuestionGrad(Kviz10Hard.)
at rs.androidaplikacije.zastaveigradovi.Kviz10Hard.access$1(Kviz10Hard.)
at rs.androidaplikacije.zastaveigradovi.Kviz10Hard$2.run(Kviz10Hard.java:68)
at android.os.Handler.handleCallback(Handler.)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.)
at android.app.ActivityThread.main(ActivityThread.)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.)
at dalvik.system.NativeStart.main(Native Method)
下面是提到的代码:

private void nextQuestionGrad() {

    flag.setVisibility(View.GONE);
    dodatnoPitanje.setVisibility(View.VISIBLE);

    TestAdapter mDbHelper = new TestAdapter(this);
    DataBaseHelper myDbHelper = new DataBaseHelper(this);

    if(!myDbHelper.checkDataBase()){
        mDbHelper.createDatabase();
    }

    try{ 

        mDbHelper.open();

        Cursor c = mDbHelper.getTestDataGradovi(mCurrentID);

        List<Answer> labels = new ArrayList<Answer>();

        labels.add(new Answer(c.getString(2), true));
        labels.add(new Answer(c.getString(3), false));
        labels.add(new Answer(c.getString(4), false));
        labels.add(new Answer(c.getString(5), false));

        tacanOdg = c.getString(2);

        Collections.shuffle(labels);

        dodatnoPitanje.setText(c.getString(1));

        bOdgovor1.setText(labels.get(0).option);
        bOdgovor1.setTag(labels.get(0));
        bOdgovor1.setOnClickListener(clickListenerGrad);

        bOdgovor2.setText(labels.get(1).option);
        bOdgovor2.setTag(labels.get(1));
        bOdgovor2.setOnClickListener(clickListenerGrad);

        bOdgovor3.setText(labels.get(2).option);
        bOdgovor3.setTag(labels.get(2));
        bOdgovor3.setOnClickListener(clickListenerGrad);

        bOdgovor4.setText(labels.get(3).option);
        bOdgovor4.setTag(labels.get(3));
        bOdgovor4.setOnClickListener(clickListenerGrad);

        score.setText("Score: " + brojacTacnihOdgovora);

    }finally{ 
        mDbHelper.close();
    }
}
有人能告诉我哪里出了问题吗?错误行(Kviz10Hard.)如下所示:


您没有检查光标是否包含任何结果。您需要检查
c.moveToNext()
的返回值,或者检查
c.getCount()
或类似的内容。

在尝试从
游标
对象获取任何数据之前,您需要迭代结果,或者如果游标只包含一个值,则需要获取第一个结果。例如,要在所有结果之间进行迭代,您应该执行以下操作:

String sql = "SELECT * FROM myTable";
Cursor cursor = mDbHelper.rawQuery(sql, null);
if(cursor != null){
    if(cursor.getCount() > 0){
        for(cursor.move(0);cursor.moveToNext();cursor.isAfterLast()){
            // get your data here
        }
    }
    cursor.close(); // close your cursor when you don't need it anymore
}
String sql = "SELECT * FROM myTable";
Cursor cursor = mDbHelper.rawQuery(sql, null);
if(cursor != null){
    cursor.moveToFirst();
    String myString = cursor.getString(cursor.getColumnIndex("title"));
    cursor.close(); // close your cursor when you don't need it anymore
}
或者另一种方法:

String sql = "SELECT * FROM myTable";
Cursor cursor = mDbHelper.rawQuery(sql, null);
if(cursor != null){
    if(cursor.getCount() > 0){
        do {
             // get value
        } while(cursor.moveToNext());
    }
    cursor.close(); // close your cursor when you don't need it anymore
}
或者,如果您非常确定根据您的sql语句,结果应该只有一个对象,那么您应该调用
cursor.moveToFirst()在从
光标访问任何内容之前,如下所示:

String sql = "SELECT * FROM myTable";
Cursor cursor = mDbHelper.rawQuery(sql, null);
if(cursor != null){
    if(cursor.getCount() > 0){
        for(cursor.move(0);cursor.moveToNext();cursor.isAfterLast()){
            // get your data here
        }
    }
    cursor.close(); // close your cursor when you don't need it anymore
}
String sql = "SELECT * FROM myTable";
Cursor cursor = mDbHelper.rawQuery(sql, null);
if(cursor != null){
    cursor.moveToFirst();
    String myString = cursor.getString(cursor.getColumnIndex("title"));
    cursor.close(); // close your cursor when you don't need it anymore
}
编辑:

如果您真的想在数据库类中创建这样的函数,我会这样做:

public static Cursor getUserTitles(int userId){
      String sql = "SELECT * FROM myTable WHERE userId=?";
      return mDbHelper.rawQuery(sql, new String[]{Integer.toString(userId)});
}
第二次编辑:

将您的
getTestDataGradovi
更改为:

public Cursor getTestDataGradovi(long id){
    String sql ="SELECT * FROM tblGradovi WHERE _ID=?";
    return  mDb.rawQuery(sql, new String[]{Integer.toString(id)});
}

并添加
c.moveToFirst(),在
光标c=mDbHelper.getTestDataGradovi(mCurrentID)之后

您的错误来自这里:游标c=mDbHelper.getTestDataGradovi(mCurrentID);检查c的值。我敢打赌是的null@AsierAranbarri
c
永远不会为
null
,但光标可能为空。不要使用直接数字访问数据。使用
c.getString(c.getColumnIndex(COLUMN_NAME_CONSTANT))
…在调用
Cursor c
的任何位置,也要编写代码
c.moveToFirst()在它之后。我不明白。我做c.moveToNext();在我的适配器类中,返回游标c。这不好吗?不。如果光标为空,这对您没有帮助!moveToNext返回的是
false
,但您没有在任何地方检查此项。那么,如何在我的代码中更正此项?这东西不是我穿的更结实的衣服,我很害怕(我已经在assets文件夹中导入了sqlite数据库。每次我从数据库中随机获得一行。首先,如果您是从assets导入数据库,请确保正确复制了整个数据库,然后使用上述方法从数据库中获取数据,任何操作都不会失败。什么是“正确复制”?我想如果它在我开始游戏时起作用,它会被复制到我的手机上。如果你的数据库中有所有的表。检查编辑的答案如何从游标中正确获取值。我不能这样做。我需要使用WHERE子句。我需要它,因为一旦我从数据库中获取一些值(问题),我就不想在单个游戏中再次使用它。