My application was running fine, until I stumbled across this SQLite error:
java.lang.IllegalMoniterStateException: attempt to unlock read lock, not locked by current thread
I've tried all the other answers on StackOverflow, but they didn't seem to help. They all said I have multiple threads creating instances of my SQLiteDBAdpdter, but I only have on Activity, but its a SherlockFragmentActivity. Each of the classes create a new instance of the SQliteDBAdapter, so I'm not sure if this is why I'm getting this error.
I've been stuck on this for hours, and would GREATLY appreciate any help.
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;
}
}
The error is pointing to db.open();
in the onCreate()
method. The open()
method is in my SQLiteDBAdapter (NotesDBAdapter)
which is as follows:
public NotesDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
And theonCreate()
in my NotesDBAdapter is:
@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);
}
}
Thanks again!
EDIT The other fragment
creating a new instance
of the database is called fragment 2, but I did not show its code as its really the same as fragment 1.