Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.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
android中的游标索引越界异常_Android_Database_Android Sqlite - Fatal编程技术网

android中的游标索引越界异常

android中的游标索引越界异常,android,database,android-sqlite,Android,Database,Android Sqlite,我的游戏中有一个记分板。仍然没有记分。它显示CursoreIndexOutOfBoundsException我该如何解决此错误。有很多解决方案,但对我不起作用。下面是我的代码 public List<Rank> getRanking() { List<Rank> listRanking = new ArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(); Cursor

我的游戏中有一个记分板。仍然没有记分。它显示CursoreIndexOutOfBoundsException我该如何解决此错误。有很多解决方案,但对我不起作用。下面是我的代码

public List<Rank> getRanking() {
    List<Rank> listRanking = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM Rank Order By Score DESC LIMIT 10;", null);
    try {

        if (c == null)
        {return null;}

        c.moveToNext();
        do {
            int Id = c.getInt(c.getColumnIndex("ID"));
            int Score = c.getInt(c.getColumnIndex("Score"));


            Rank ranking = new Rank(Id, Score);
            listRanking.add(ranking);
        } while (c.moveToNext());

    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        c.close();
        db.close();
    }
    return listRanking;

}
public List getRanking(){
List listRanking=新建ArrayList();
SQLiteDatabase db=this.getReadableDatabase();
游标c=db.rawQuery(“按分数描述限制10从排名顺序中选择*”,空);
试一试{
如果(c==null)
{返回null;}
c、 moveToNext();
做{
int Id=c.getInt(c.getColumnIndex(“Id”);
int Score=c.getInt(c.getColumnIndex(“Score”);
排名=新排名(Id、分数);
添加(排名);
}而(c.moveToNext());
}捕获(例外e){
e、 printStackTrace();
}
最后{
c、 close();
db.close();
}
返回列表排名;
}

有人能帮我解决这个问题吗?任何帮助或建议都将不胜感激。谢谢。

尝试将第一个c.moveToNext()更改为c.moveToFirst():

public List getRanking(){
List listRanking=新建ArrayList();
SQLiteDatabase db=this.getReadableDatabase();
游标c=db.rawQuery(“按分数描述限制10从排名顺序中选择*”,空);
试一试{
如果(c==null)
{返回null;}
//将moveToNext()更改为moveToFirst()
if(c.moveToFirst()){
做{
int Id=c.getInt(c.getColumnIndex(“Id”);
int Score=c.getInt(c.getColumnIndex(“Score”);
排名=新排名(Id、分数);
添加(排名);
}而(c.moveToNext());
}
}捕获(例外e){
e、 printStackTrace();
}
最后{
c、 close();
db.close();
}
返回列表排名;
}

问题是,当没有下一个时,您正在移动到下一个。你需要检查并确保有下一个去。另外,去掉第一个
c.moveToNext()
。你不需要它。它使您跳过第一个值。只要做:

       try {

        if (c == null){
             return null;
        }
        do {
            int Id = c.getInt(c.getColumnIndex("ID"));
            int Score = c.getInt(c.getColumnIndex("Score"));


            Rank ranking = new Rank(Id, Score);
            listRanking.add(ranking);
            if(c.moveToNext() == null){
                 break;
             }
        } while (c.moveToNext());

请尝试使用此选项,它将起作用:-

public List<Rank> getRanking() {
    List<Rank> listRanking = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM Rank Order By Score DESC LIMIT 10;", null);
    try {

        if (c == null)
        {return null;}

        while (c.moveToNext()) {
            int index = c.getColumnIndex("ID");
            int Id = 0;
            if (-1 != index) {
                Id = c.getInt(index);
            }
            index = c.getColumnIndex("Score");
            int Score = 0;
            if (-1 != index) {
                Score = c.getInt(index1);
            }


            Rank ranking = new Rank(Id, Score);
            listRanking.add(ranking);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (null != c) {
            c.close();
        }
        db.close();
    }
    return listRanking;

}
public List getRanking(){
List listRanking=新建ArrayList();
SQLiteDatabase db=this.getReadableDatabase();
游标c=db.rawQuery(“按分数描述限制10从排名顺序中选择*”,空);
试一试{
如果(c==null)
{返回null;}
while(c.moveToNext()){
int index=c.getColumnIndex(“ID”);
int Id=0;
如果(-1!=索引){
Id=c.getInt(索引);
}
索引=c.getColumnIndex(“分数”);
智力得分=0;
如果(-1!=索引){
分数=c.getInt(index1);
}
排名=新排名(Id、分数);
添加(排名);
}
}捕获(例外e){
e、 printStackTrace();
}
最后{
if(null!=c){
c、 close();
}
db.close();
}
返回列表排名;
}

@AnkitGusain您从哪里得到错误?哪一行?在android monitor中,在do语句之后,我将检查
c.getColumnIndex(“ID”)的数值,并对照光标计数检查该数字。看起来调用超出了光标的范围。我猜你得到的是-1,这就是你得到OOB错误的原因。这样做不起作用应用程序被强制停止你确定你使用的代码与创建一个r.super.onCreate(savedInstanceState)的代码相同吗;setContentView(R.layout.activity_分数);textView=(textView)findViewById(R.id.textView);setText(“分数”);lstView=(ListView)findViewById(R.id.lstRanking);DbHelper db=新的DbHelper(this);List lstRanking=db.getRanking();如果(lstRanking.size()>0){CustomAdapter adapter=new CustomAdapter(this,lstRanking);lstView.setAdapter(adapter);}}并且建议在后台线程中执行数据库操作,因为它会阻止创建ANR的主线程。让我们来看看。
public List<Rank> getRanking() {
    List<Rank> listRanking = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM Rank Order By Score DESC LIMIT 10;", null);
    try {

        if (c == null)
        {return null;}

        while (c.moveToNext()) {
            int index = c.getColumnIndex("ID");
            int Id = 0;
            if (-1 != index) {
                Id = c.getInt(index);
            }
            index = c.getColumnIndex("Score");
            int Score = 0;
            if (-1 != index) {
                Score = c.getInt(index1);
            }


            Rank ranking = new Rank(Id, Score);
            listRanking.add(ranking);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (null != c) {
            c.close();
        }
        db.close();
    }
    return listRanking;

}