我的应用程序运行良好,直到我偶然发现了这个 SQLite 错误:
java.lang.IllegalMoniterStateException: attempt to unlock read lock, not locked by current thread
我已经尝试了 StackOverflow 上的所有其他答案,但它们似乎没有帮助。他们都说我有多个线程创建 SQLiteDBAdpdter 的实例,但我只有 Activity,但它是 SherlockFragmentActivity。每个类都会创建 SQliteDBAdapter 的一个新实例,因此我不确定这是否就是出现此错误的原因。
我已经在这个问题上坚持了几个小时了,并且非常非常感谢任何帮助。
fragment 1:
public final class NotesFragment extends SherlockListFragment {
NotesDbAdapter db;
private static final int ACTIVITY_EDIT = 1;
private static final int DELETE_ID = Menu.FIRST + 1;
public static NotesFragment newInstance(String content) {
NotesFragment fragment = new NotesFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new NotesDbAdapter(getActivity());
db.open();
if (db.fetchAllNotes().getCount() == 0) {
doWelcomeMessage();
}
db.close();
}
private void doWelcomeMessage() {
// Show welcome dialog
startActivity(new Intent(getActivity(), NotesWelcome.class));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
db = new NotesDbAdapter(getActivity());
db.open();
if (db.fetchAllNotes().getCount() == 0) {
doWelcomeMessage();
}
db.close();
return ll;
}
}
错误指向 onCreate()
方法中的 db.open();
。 open()
方法位于我的 SQLiteDBAdapter (NotesDBAdapter)
中,如下所示:
public NotesDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
我的 NotesDBAdapter 中的 onCreate()
是:
@Override
public void onCreate(SQLiteDatabase db) {
SQLiteDatabase checkDB = null;
String myPath = "data/data/com.xxx.xxx/databases/data.db";
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
if (checkDB != null) {
// exists; do nothing
} else {
// create DB
db.execSQL(DATABASE_CREATE);
}
}
再次感谢!
编辑 另一个 fragment
creating a new instance
该数据库的名称为 fragment 2,但我没有显示其代码,因为它确实与 fragment 1 相同。
最佳答案
我不确定这是否是问题所在,但似乎您打开同一个数据库两次进行写入。请打开一个并在您的应用程序中共享它。
关于java - Android SQLite尝试解锁读锁,当前线程未锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16018395/