Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.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,我收到应用程序的崩溃报告错误。我在assets文件夹中使用导入的sqlite数据库。我做错了什么?以下是用户崩溃报告中的错误: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418) at android.database.Abstra

我收到应用程序的崩溃报告错误。我在assets文件夹中使用导入的sqlite数据库。我做错了什么?以下是用户崩溃报告中的错误:

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at rs.androidaplikacije.zastaveigradovi.Kviz20Hard.nextQuestionGrad(Kviz20Hard.java:328)
at rs.androidaplikacije.zastaveigradovi.Kviz20Hard.access$1(Kviz20Hard.java:307)
at rs.androidaplikacije.zastaveigradovi.Kviz20Hard$2.run(Kviz20Hard.java:68)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
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);
                c.moveToFirst();

                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();
            }
     }
下面是我的适配器类:

public class TestAdapter 
{
    protected static final String TAG = "DataAdapter";

    private final Context mContext;
    private SQLiteDatabase mDb;
    private DataBaseHelper mDbHelper;

    public TestAdapter(Context context) 
    {
        this.mContext = context;
        mDbHelper = new DataBaseHelper(mContext);
    }

    public TestAdapter createDatabase() throws SQLException 
    {
        try 
        {
            mDbHelper.createDataBase();
        } 
        catch (IOException mIOException) 
        {
            Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
            throw new Error("UnableToCreateDatabase");
        }
        return this;
    }

    public TestAdapter open() throws SQLException 
    {
        try 
        {
            mDbHelper.openDataBase();
            mDbHelper.close();
            mDb = mDbHelper.getReadableDatabase();
        } 
        catch (SQLException mSQLException) 
        {
            Log.e(TAG, "open >>"+ mSQLException.toString());
            throw mSQLException;
        }
        return this;
    }

    public void close() 
    {
        mDbHelper.close();
    }

     public Cursor getTestData(String whereClause){
             String sql ="SELECT * FROM tblPitanja WHERE 1 = 1 " + whereClause + " ORDER BY RANDOM() LIMIT 1";
             return mDb.rawQuery(sql, null);
         }

     public Cursor getTestDataGradovi(long id){
            String sql ="SELECT * FROM tblGradovi WHERE _ID = " + id;
            return  mDb.rawQuery(sql, null);
        }

     public Cursor getTestDataValute(long id){
             String sql ="SELECT * FROM tblValute WHERE _ID = " + id;

             return mDb.rawQuery(sql, null);
         }

}
android.database.CursorIndexOutOfBoundsException:请求索引0, 大小为0的

您的问题很可能是您的
c.moveToFirst()
方法返回了false(它指示游标是否为空),因此您得到了
异常

无论何时处理游标,请始终检查
moveToFirst()
方法返回的值,该方法表示“方便”工具如何根据条件检测游标的有效性,并将“操作”包装到此条件:

List<Answer> labels = new ArrayList<Answer>();
if (c != null) {
   if (c.moveToFirst()) {
      // do your stuff
      ...
      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));
      ...
   }
   else {
     // Cursor is empty
   }
}
else {
   // Cursor is null
}
List labels=new ArrayList();
如果(c!=null){
if(c.moveToFirst()){
//做你的事
...
添加(新答案(c.getString(2),true));
添加(新答案(c.getString(3),false));
添加(新答案(c.getString(4),false));
添加(新答案(c.getString(5),false));
...
}
否则{
//光标为空
}
}
否则{
//游标为空
}

注意:当您想要从游标中包含的特定列检索值时,我建议您使用
getColumnIndex()
方法来避免“键入”和“索引”问题。这是一种非常好且有效的做法。

您试图访问
光标
数据,但它似乎是空的。使用
c.moveToFirst()
实际检查光标是否为空-它返回一个布尔值指示:
boolean isFilledWithData=c.moveToFirst()

,当光标为空时,代码的其他部分该怎么办?当光标为空时会发生什么?@marjanbaz可能会为您向logcat显示一些错误消息?那你就知道问题出在哪里了。若游标为空,则数据库可能不包含数据或特定行。如果是
NULL
,问题在于
SQLiteDatabase
。我不明白为什么光标是空的。我总共有197行6列。每次我随机画一行。我是在nextQuestionGrad()中声明还是作为类变量声明?
List<Answer> labels = new ArrayList<Answer>();
if (c != null) {
   if (c.moveToFirst()) {
      // do your stuff
      ...
      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));
      ...
   }
   else {
     // Cursor is empty
   }
}
else {
   // Cursor is null
}