我的应用程序运行良好,直到我偶然发现这个 SQLite 错误:
java.lang.IllegalMoniterStateException: attempt to unlock read lock, not locked by current thread
我已经尝试过 StackOverflow 上的所有其他答案,但它们似乎没有帮助。他们都说我有多个线程创建我的 SQLiteDBAdpdter 实例,但我只有 Activity,但它是一个 SherlockFragmentActivity。每个类都创建了一个新的 SQliteDBAdapter 实例,所以我不确定这是否是我收到此错误的原因。
我已经坚持了几个小时,非常感谢任何帮助。
片段 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;
}
}
错误指向方法db.open();
中onCreate()
。我的open()
方法SQLiteDBAdapter (NotesDBAdapter)
如下:
public NotesDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
在onCreate()
我的 NotesDBAdapter 中是:
@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);
}
}
再次感谢!
编辑 另一个片段
创建一个新实例
数据库的代码称为片段 2,但我没有显示它的代码,因为它与片段 1 完全相同。