2013-05-13 148 views
0

我的应用程序崩溃报告错误。我在资产文件夹中使用导入的sqlite数据库。我究竟做错了什么?下面是从用户的崩溃报告的错误:android.database.CursorIndexOutOfBoundsException ....崩溃报告

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

我得到行错误:

labels.add(new Answer(c.getString(2), true)); 

这里是我的适配器类:

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

} 

回答

0

android.database.CursorIndexOutOfBoundsException:指数0要求, 大小为0

您的问题是最有可能您c.moveToFirst()方法返回false(它指示光标是否为空),并为这个原因,你越来越Exception

每当你使用游标处理总是从一个表示“方便”工具如何检测光标的有效性与条件和包装你的“行动”这一条件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 
} 

注意:当您想要从光标所包含的特定列中检索值时,我建议您使用getColumnIndex(<columnName>)方法来避免“错字”和“索引”问题。这是非常好的和有效的做法。

+0

当游标为空时,你的代码的其他部分如何处理?什么时候光标是空的? – marjanbaz 2013-05-13 16:48:59

+0

@marjanbaz show可能会给logcat一些错误信息给你?那么你知道问题在哪里。如果游标为空,则可能您的数据库不包含数据或特定行。如果是NULL,问题出现在SQLiteDatabase中。 – Sajmon 2013-05-13 16:54:30

+0

我只是不明白为什么光标会变空。我有197列6列。每次我画一个随机的行。 – marjanbaz 2013-05-13 17:43:07

0

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

+0

我是否在我的nextQuestionGrad()中声明了这个或者是作为一个类变量? – marjanbaz 2013-05-13 16:51:13