我的应用程序出现崩溃报告错误。我在资源文件夹中使用导入的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();
}
}
我在网上收到错误:
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 :(得分: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
}
注意:当您想要从Cursor中包含的特定列中检索值时,我建议您使用getColumnIndex(<columnName>)
方法来避免“拼写错误”和“索引”问题。这是非常好的和有效的练习。
答案 1 :(得分:0)
您正在尝试访问Cursor
数据,但它似乎是空的。使用c.moveToFirst()
来实际检查游标是否为空 - 它返回一个布尔指示:boolean isFilledWithData = c.moveToFirst()