我正在尝试创建一个登录页面,并使用SQLiteOpenHelper类连接到SQLite数据库。但是SQLiteOpenHelper.java中的默认代码在db.reopenReadWrite()行显示错误;我检查了SQLiteDatabase.java文件是否有reopenReadWrite()方法;您能帮我解决这个问题吗
发布于 2017-10-11 18:37:06
正如您在the source code中看到的那样,此方法通过@hide javadoc attribute隐藏
/**
* Reopens the database in read-write mode.
* If the database is already read-write, does nothing.
*
* @throws SQLiteException if the database could not be reopened as requested, in which
* case it remains open in read only mode.
* @throws IllegalStateException if the database is not open.
*
* @see #isReadOnly()
* @hide
*/
public void reopenReadWrite() {
synchronized (mLock) {
throwIfNotOpenLocked();
if (!isReadOnlyLocked()) {
return; // nothing to do
}
// Reopen the database in read-write mode.
final int oldOpenFlags = mConfigurationLocked.openFlags;
mConfigurationLocked.openFlags = (mConfigurationLocked.openFlags & ~OPEN_READ_MASK)
| OPEN_READWRITE;
try {
mConnectionPoolLocked.reconfigure(mConfigurationLocked);
} catch (RuntimeException ex) {
mConfigurationLocked.openFlags = oldOpenFlags;
throw ex;
}
}
}
因为它不是公共API的一部分,所以不能在不使用反射的情况下使用该方法。
最好的方法是只使用公共API listed in the SQLiteDatabase documentation中的方法。
您所需要的只是SQLiteOpenHelper#getReadableDatabase()
方法,该方法依次调用getDatabaseLocked()
,后者在内部调用db.reopenReadWrite()
public SQLiteDatabase getReadableDatabase() {
synchronized (this) {
return getDatabaseLocked(false);
}
}
private SQLiteDatabase getDatabaseLocked(boolean writable) {
if (mDatabase != null) {
if (!mDatabase.isOpen()) {
// Darn! The user closed the database by calling mDatabase.close().
mDatabase = null;
} else if (!writable || !mDatabase.isReadOnly()) {
// The database is already open for business.
return mDatabase;
}
}
if (mIsInitializing) {
throw new IllegalStateException("getDatabase called recursively");
}
SQLiteDatabase db = mDatabase;
try {
mIsInitializing = true;
if (db != null) {
if (writable && db.isReadOnly()) {
db.reopenReadWrite();
}
//.......................
https://stackoverflow.com/questions/46695162
复制相似问题