我遇到了getReadableDatabase()方法的问题。在我的一个方法中调用时,它会抛出:
06-12 13:58:54.000: E/AndroidRuntime(31845): Caused by: java.lang.IllegalStateException: getDatabase called recursively
以下是完整的LogCat输出:
06-12 13:58:54.000: E/AndroidRuntime(31845): FATAL EXCEPTION: main
06-12 13:58:54.000: E/AndroidRuntime(31845): java.lang.IllegalStateException: Could not execute method of the activity
06-12 13:58:54.000: E/AndroidRuntime(31845): at android.view.View$1.onClick(View.java:3691)
06-12 13:58:54.000: E/AndroidRuntime(31845): at android.view.View.performClick(View.java:4211)
06-12 13:58:54.000: E/AndroidRuntime(31845): at android.view.View$PerformClick.run(View.java:17267)
06-12 13:58:54.000: E/AndroidRuntime(31845): at android.os.Handler.handleCallback(Handler.java:615)
06-12 13:58:54.000: E/AndroidRuntime(31845): at android.os.Handler.dispatchMessage(Handler.java:92)
06-12 13:58:54.000: E/AndroidRuntime(31845): at android.os.Looper.loop(Looper.java:137)
06-12 13:58:54.000: E/AndroidRuntime(31845): at android.app.ActivityThread.main(ActivityThread.java:4898)
06-12 13:58:54.000: E/AndroidRuntime(31845): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 13:58:54.000: E/AndroidRuntime(31845): at java.lang.reflect.Method.invoke(Method.java:511)
06-12 13:58:54.000: E/AndroidRuntime(31845): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
06-12 13:58:54.000: E/AndroidRuntime(31845): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
06-12 13:58:54.000: E/AndroidRuntime(31845): at dalvik.system.NativeStart.main(Native Method)
06-12 13:58:54.000: E/AndroidRuntime(31845): Caused by: java.lang.reflect.InvocationTargetException
06-12 13:58:54.000: E/AndroidRuntime(31845): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 13:58:54.000: E/AndroidRuntime(31845): at java.lang.reflect.Method.invoke(Method.java:511)
06-12 13:58:54.000: E/AndroidRuntime(31845): at android.view.View$1.onClick(View.java:3686)
06-12 13:58:54.000: E/AndroidRuntime(31845): ... 11 more
06-12 13:58:54.000: E/AndroidRuntime(31845): Caused by: java.lang.IllegalStateException: getDatabase called recursively
06-12 13:58:54.000: E/AndroidRuntime(31845): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:204)
06-12 13:58:54.000: E/AndroidRuntime(31845): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
06-12 13:58:54.000: E/AndroidRuntime(31845): at com.example.haw_app.stisysManager.persistenz.SQLiteConnector.getDBNames(SQLiteConnector.java:538)
06-12 13:58:54.000: E/AndroidRuntime(31845): at com.example.haw_app.stisysManager.persistenz.SQLiteConnector.onCreate(SQLiteConnector.java:53)
06-12 13:58:54.000: E/AndroidRuntime(31845): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
06-12 13:58:54.000: E/AndroidRuntime(31845): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
06-12 13:58:54.000: E/AndroidRuntime(31845): at com.example.haw_app.stisysManager.persistenz.SQLiteConnector.getStudent(SQLiteConnector.java:501)
06-12 13:58:54.000: E/AndroidRuntime(31845): at com.example.haw_app.stisysManager.persistenz.SQLiteConnector.setAverageGrade(SQLiteConnector.java:448)
06-12 13:58:54.000: E/AndroidRuntime(31845): at com.example.haw_app.stisysManager.persistenz.SqlTest.testStudent(SqlTest.java:56)
06-12 13:58:54.000: E/AndroidRuntime(31845): at com.example.haw_app.stisysManager.persistenz.SqlTest.runTest(SqlTest.java:29)
06-12 13:58:54.000: E/AndroidRuntime(31845): at com.example.haw_app.SchnitzeljagdActivity.getCoordinatesClick(SchnitzeljagdActivity.java:109)
06-12 13:58:54.000: E/AndroidRuntime(31845): ... 14 more
我已经读过在onCreate或onUpgrade方法中使用getReadableDatabase或getWritableDatabase时出现此问题。但遗憾的是,这不是我的问题。
以下是一些代码:
public class SQLiteConnector extends SQLiteOpenHelper {
public SQLiteConnector(Context context) {
super(context, dbName, null, dbversion);
}
private static final String dbName = "StiSysManagerDB";
private static final int dbversion = 2;
private static final String[] tableName = new String[] {"Student","NextApplicationDates",TABLE_COURSES,
"Trainings","SolvedTests","SolvedTrainings","Mailinglists"};
private static final String[] columns = new String[] {
"( name VARCHAR , avgGrade REAL , userName VARCHAR , password VARCHAR , matNr INT , pwExpDate VARCHAR , birthday VARCHAR , printCredit INT )",
"( type VARCHAR , fromDate VARCHAR , toDate VARCHAR )",//NextApplicationDates
"( name VARCHAR , type INT , prof VARCHAR , status VARCHAR )",//Courses
"( name VARCHAR , prof VARCHAR , status VARCHAR , "+KEY_GROUP+" INT )", //registeredTrainings
"( name VARCHAR , grade INT )",//solvedTests
"( name VARCHAR , status INT )", //solvedTrainings
"( name VARCHAR , adress VARCHAR )"//mailinglists
};
@Override
public void onCreate(SQLiteDatabase db) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i<tableName.length;i++)
{
sb.delete(0, sb.length());
sb.append("CREATE TABLE IF NOT EXISTS ").append(tableName[i]).append(" ").append(columns[i]).append(";");
db.execSQL(sb.toString());
}
String[] sary = this.getDBNames();
for (String s : sary)
{
Log.d("DBNames", "s");
}
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int oldVersion, int newVersion) {
if (oldVersion == 1 && newVersion >= 2)
{
arg0.execSQL("alter table Student add column password VARCHAR default ''");
}
}
private int saveStudent(Student std)
{
ContentValues val = new ContentValues();
val.put(Student.NAME, std.name);
val.put(Student.AVERAGEGRADE, std.avgGrade);
val.put(Student.USERNAME, std.userName);
val.put(Student.MATRIKELNUMMER, std.matNr);
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
val.put(Student.PASSWORD_EXP_DATE, fmt.print(std.pwExpDate));
val.put(Student.BIRTHDAY, fmt.print(std.birthdate));
val.put(Student.PRINTCREDIT, std.printCredit);
val.put(Student.PASSWORD, std.pw);
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Student.TABLENAME, null, null);
int i = (int)(db.insert(Student.TABLENAME, null, val));
db = null;
return i;
}
private Student getStudent()
{
SQLiteDatabase db = this.getReadableDatabase();
Student std = new Student();
Cursor cursor = db.rawQuery("SELECT "+Student.columnList()+" FROM Student", null);
db = null;
if (cursor.getCount()!=0)
{
cursor.moveToNext();
std.name = cursor.getString(cursor.getColumnIndex(Student.NAME));
std.avgGrade = cursor.getDouble(cursor.getColumnIndex(Student.AVERAGEGRADE));
std.userName = cursor.getString(cursor.getColumnIndex(Student.USERNAME));
std.matNr = cursor.getInt(cursor.getColumnIndex(Student.MATRIKELNUMMER));
String pwExpDate = cursor.getString(cursor.getColumnIndex(Student.PASSWORD_EXP_DATE));
std.pwExpDate= Util.createDateTimeFromString(pwExpDate);
String birthday = cursor.getString(cursor.getColumnIndex(Student.BIRTHDAY));
std.birthdate= Util.createDateTimeFromString(birthday);
std.printCredit = cursor.getInt(cursor.getColumnIndex(Student.PRINTCREDIT));
std.pw = cursor.getString(cursor.getColumnIndex(Student.PASSWORD));
}else{
saveStudent(std);
}
return std;
}
}
学生班:
class Student{
static final String NAME = "name", USERNAME = "userName", AVERAGEGRADE ="avgGrade", MATRIKELNUMMER = "matNr",
PASSWORD_EXP_DATE = "pwExpDate", BIRTHDAY = "birthday", PRINTCREDIT = "printCredit", PASSWORD = "password";
static final String TABLENAME ="Student";
String name,userName,pw;
double avgGrade;
int matNr, printCredit;
DateTime birthdate, pwExpDate;
static String columnList()
{
StringBuilder sb = new StringBuilder();
sb.append(NAME).append(", ").append(USERNAME).append(", ").append(AVERAGEGRADE).append(", ").append(MATRIKELNUMMER)
.append(", ").append(PASSWORD_EXP_DATE).append(", ").append(BIRTHDAY).append(", ").append(PRINTCREDIT).append(", ").append(PASSWORD);
return sb.toString();
}
public Student()
{
name = null;
userName = null;
avgGrade = 0;
matNr = 0;
printCredit = 0;
birthdate = null;
pwExpDate = null;
pw = null;
}
}
我做错了什么吗?哦,并且:我不在此类之外使用getReadableDatabase或getWritableDatabase。
任何提示?