因此,我有一个名为 DatabaseHandler 的类,它继承了 SQLiteOpenHelper。该课程负责管理将信息从在线数据库复制到手机的数据库。现在,问题是当我调用它的getQuestionsCount()
方法时,顾名思义,它计算项目的数量,它崩溃了。它在调用其getReadableDatabase()
方法的行上崩溃。它告诉我它有一个 IllegalStateException。这是怎么回事?
崩溃活动的代码。检查它是否已经存储了问题以供使用:
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
if(db.getQuestionsCount() == 0)
if(!isNetworkAvailable() || !canConnectToServer())
return -2;
else
if(!isNetworkAvailable() || !canConnectToServer())
return -3;
我的 DatabaseHandler 类的一部分:
public class DatabaseHandler extends SQLiteOpenHelper
{
...
public DatabaseHandler(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
...
@Override
public void onCreate(SQLiteDatabase db)
{
String CREATE_QUESTIONS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_QUESTIONS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_QUESTION + " TEXT NOT NULL,"
+ KEY_CATEGORY + " INTEGER NOT NULL," + KEY_ANSWER_1 + " TEXT NOT NULL,"
+ KEY_ANSWER_2 + " TEXT NOT NULL," + KEY_ANSWER_3 + " TEXT NOT NULL,"
+ KEY_ANSWER_4 + " TEXT NOT NULL," + KEY_DATE_POSTED + " TEXT NOT NULL)";
db.execSQL(CREATE_QUESTIONS_TABLE);
db.close();
}
...
public int getQuestionsCount()
{
String countQuery = "SELECT * FROM " + TABLE_QUESTIONS;
SQLiteDatabase db = this.getReadableDatabase(); //<-- ERROR ON THIS LINE
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
// return count
return count;
}
}