我最近一直收到这个错误,我知道原因,但我不知道如何解决它。
这是来自 logcat 的错误:
10-05 02:34:00.177: E/SQLiteDatabase(1111): close() was never explicitly called on database '/data/data/standard.internet.marketing.mymovingfriend/databases/mymovingfriend'
10-05 02:34:00.177: E/SQLiteDatabase(1111): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
10-05 02:34:00.177: E/SQLiteDatabase(1111): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:770)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at standard.internet.marketing.mymovingfriend.SQLHandler.open(SQLHandler.java:178)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at standard.internet.marketing.mymovingfriend.ListMovingNames$1.onClick(ListMovingNames.java:71)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at android.view.View.performClick(View.java:3511)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at android.view.View$PerformClick.run(View.java:14105)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at android.os.Handler.handleCallback(Handler.java:605)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at android.os.Handler.dispatchMessage(Handler.java:92)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at android.os.Looper.loop(Looper.java:137)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at java.lang.reflect.Method.invokeNative(Native Method)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at java.lang.reflect.Method.invoke(Method.java:511)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-05 02:34:00.177: E/SQLiteDatabase(1111): at dalvik.system.NativeStart.main(Native Method)
10-05 02:34:00.216: E/System(1111): Uncaught exception thrown by finalizer
10-05 02:34:00.226: E/System(1111): java.lang.IllegalStateException: Don't have database lock!
10-05 02:34:00.226: E/System(1111): at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2090)
10-05 02:34:00.226: E/System(1111): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2182)
10-05 02:34:00.226: E/System(1111): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2178)
10-05 02:34:00.226: E/System(1111): at android.util.LruCache.trimToSize(LruCache.java:197)
10-05 02:34:00.226: E/System(1111): at android.util.LruCache.evictAll(LruCache.java:285)
10-05 02:34:00.226: E/System(1111): at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2143)
10-05 02:34:00.226: E/System(1111): at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1126)
10-05 02:34:00.226: E/System(1111): at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1914)
10-05 02:34:00.226: E/System(1111): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
10-05 02:34:00.226: E/System(1111): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
10-05 02:34:00.226: E/System(1111): at java.lang.Thread.run(Thread.java:856)
这是活动的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selectorcreatemove);
allFunctions();
}
private void allFunctions() {
addMove = (Button) findViewById(R.id.bAddMove);
etAddMove = (EditText) findViewById(R.id.etMoveName);
MoveList = (ListView) findViewById(R.id.lvMoveItems);
SQLHandlerview = new SQLHandler(this);
SQLHandlerview = new SQLHandler(ListMovingNames.this);
SQLHandlerview.open();
cursor = SQLHandlerview.getMove();
startManagingCursor(cursor);
String[] from = new String[]{SQLHandler.KEY_MOVENAME};
int[] to = new int[]{R.id.text};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
MoveList.setAdapter(cursorAdapter);
SQLHandlerview.close();
addMove.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
public void onClick(View v) {
// TODO Auto-generated method stub
String ssmoveName = etAddMove.getText().toString();
int x = ssmoveName.length() - 1;
if (ssmoveName.isEmpty() || Character.isWhitespace(ssmoveName.charAt(0)) || Character.isWhitespace(ssmoveName.charAt(x))) {
Toast.makeText(ListMovingNames.this,
"Please enter a valid name! Avoid giving a blank name or white space at the beginning or end of the name",
Toast.LENGTH_LONG).show();
}else{
try {
SQLHandler check = new SQLHandler(ListMovingNames.this);
check.open();
String scheck = check.checkMove(ssmoveName);
check.close();
if (scheck.equals(ssmoveName)) {
Toast.makeText(ListMovingNames.this, "Move name already exist please give a different name", Toast.LENGTH_LONG).show();
} else{
SQLHandler entry = new SQLHandler(ListMovingNames.this);
entry.open();
entry.createMove(ssmoveName);
entry.setTodo(ssmoveName);
Toast.makeText(ListMovingNames.this, "Okay", Toast.LENGTH_SHORT).show();
entry.close();
Intent i = new Intent(getApplicationContext(), StartMoving.class);
i.putExtra("moveName", ssmoveName);
startActivity(i);
}
} catch (Exception e) {
// TODO Auto-generated catch block
SQLHandler entry = new SQLHandler(ListMovingNames.this);
entry.open();
entry.createMove(ssmoveName);
entry.setTodo(ssmoveName);
entry.close();
Intent i = new Intent(getApplicationContext(), StartMoving.class);
i.putExtra("moveName", ssmoveName);
startActivity(i);
}
}
}
});
MoveList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, final View view, int position,
long id) {
// TODO Auto-generated method stub
move = ((TextView) view).getText().toString();
Intent i = new Intent(getApplicationContext(), StartMoving.class);
i.putExtra("moveName", move);
startActivity(i);
}
});
}
}
每次调用这行代码时都会出现错误。entry.setTodo(ssmoveName);
这个方法调用来自我的数据库处理程序。
public long setTodo(String smovename){
ContentValues cv = new ContentValues();
cv.put(KEY_TODOMOVE, smovename);
cv.put(KEY_CB1, "Not Done");
cv.put(KEY_CB2, "Not Done");
cv.put(KEY_CB3, "Not Done");
cv.put(KEY_CB4, "Not Done");
cv.put(KEY_CB5, "Not Done");
cv.put(KEY_CB6, "Not Done");
cv.put(KEY_CB7, "Not Done");
cv.put(KEY_CB8, "Not Done");
cv.put(KEY_CB9, "Not Done");
cv.put(KEY_CB10, "Not Done");
cv.put(KEY_CB11, "Not Done");
cv.put(KEY_CB12, "Not Done");
cv.put(KEY_CB13, "Not Done");
cv.put(KEY_CB14, "Not Done");
cv.put(KEY_CB15, "Not Done");
cv.put(KEY_CB16, "Not Done");
cv.put(KEY_CB17, "Not Done");
cv.put(KEY_CB18, "Not Done");
cv.put(KEY_CB19, "Not Done");
cv.put(KEY_CB20, "Not Done");
cv.put(KEY_CB21, "Not Done");
cv.put(KEY_CB22, "Not Done");
cv.put(KEY_CB23, "Not Done");
cv.put(KEY_CB24, "Not Done");
cv.put(KEY_CB25, "Not Done");
cv.put(KEY_CB26, "Not Done");
cv.put(KEY_CB27, "Not Done");
cv.put(KEY_CB28, "Not Done");
cv.put(KEY_CB29, "Not Done");
cv.put(KEY_CB30, "Not Done");
cv.put(KEY_CB31, "Not Done");
cv.put(KEY_CB32, "Not Done");
cv.put(KEY_CB33, "Not Done");
cv.put(KEY_CB34, "Not Done");
cv.put(KEY_CB35, "Not Done");
cv.put(KEY_CB36, "Not Done");
cv.put(KEY_CB37, "Not Done");
cv.put(KEY_CB38, "Not Done");
return ourDatabase.insert(DATABASE_TABLE4, null, cv);
}
无论如何,这是我的数据库处理程序。
public class SQLHandler {
public static final String KEY_ROOMMOVEHOLDER = "roommoveholder";
public static final String KEY_ROOM = "room";
public static final String KEY_ROOMWEIGHT = "roomweight";
public static final String KEY_ITEMMOVEHOLDER = "itemmoveholder";
public static final String KEY_ITEMNAME = "itemname";
public static final String KEY_ITEMVALUE = "itemvalue";
public static final String KEY_ROOMHOLDER = "roomholder";
public static final String KEY_ITEMWEIGHT = "itemweight";
public static final String KEY_MOVENAME = "movename";
public static final String KEY_ID1 = "_id";
public static final String KEY_ID2 = "_id";
public static final String KEY_ID3 = "_id";
public static final String KEY_ID4 = "_id";
public static final String KEY_MOVEDATE = "movedate";
public static final String KEY_TOTALMOVEWEIGHT = "totalmoveweight";
private static final String DATABASE_NAME = "mymovingfriend";
private static final int DATABASE_VERSION = 1;
public static final String KEY_TODOMOVE = "todomove";
public static final String KEY_CB1 = "cb1";
public static final String KEY_CB2 = "cb2";
public static final String KEY_CB3 = "cb3";
public static final String KEY_CB4 = "cb4";
public static final String KEY_CB5 = "cb5";
public static final String KEY_CB6 = "cb6";
public static final String KEY_CB7 = "cb7";
public static final String KEY_CB8 = "cb8";
public static final String KEY_CB9 = "cb9";
public static final String KEY_CB10 = "cb10";
public static final String KEY_CB11 = "cb11";
public static final String KEY_CB12 = "cb12";
public static final String KEY_CB13 = "cb13";
public static final String KEY_CB14 = "cb14";
public static final String KEY_CB15 = "cb15";
public static final String KEY_CB16 = "cb16";
public static final String KEY_CB17 = "cb17";
public static final String KEY_CB18 = "cb18";
public static final String KEY_CB19 = "cb19";
public static final String KEY_CB20 = "cb20";
public static final String KEY_CB21 = "cb21";
public static final String KEY_CB22 = "cb22";
public static final String KEY_CB23 = "cb23";
public static final String KEY_CB24 = "cb24";
public static final String KEY_CB25 = "cb25";
public static final String KEY_CB26 = "cb26";
public static final String KEY_CB27 = "cb27";
public static final String KEY_CB28 = "cb28";
public static final String KEY_CB29 = "cb29";
public static final String KEY_CB30 = "cb30";
public static final String KEY_CB31 = "cb31";
public static final String KEY_CB32 = "cb32";
public static final String KEY_CB33 = "cb33";
public static final String KEY_CB34 = "cb34";
public static final String KEY_CB35 = "cb35";
public static final String KEY_CB36 = "cb36";
public static final String KEY_CB37 = "cb37";
public static final String KEY_CB38 = "cb38";
private static final String DATABASE_TABLE1 = "movingname";
private static final String DATABASE_TABLE2 = "movingrooms";
private static final String DATABASE_TABLE3 = "movingitems";
private static final String DATABASE_TABLE4 = "todolist";
public static final String CREATE_TABLE_1 = "CREATE TABLE " + DATABASE_TABLE1 + " (" +
KEY_ID1 + " INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_MOVEDATE + " TEXT NOT NULL, " +
KEY_TOTALMOVEWEIGHT + " TEXT NOT NULL, " +
KEY_MOVENAME + " TEXT NOT NULL);";
public static final String CREATE_TABLE_2 = "CREATE TABLE " + DATABASE_TABLE2 + " (" +
KEY_ID2 + " INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_ROOMMOVEHOLDER + " TEXT NOT NULL, " +
KEY_ROOMWEIGHT + " TEXT NOT NULL, " +
KEY_ROOM + " TEXT NOT NULL);";
public static final String CREATE_TABLE_3 = "CREATE TABLE " + DATABASE_TABLE3 + " (" +
KEY_ID3 + " INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_ITEMNAME + " TEXT NOT NULL, " +
KEY_ITEMVALUE + " TEXT NOT NULL, " +
KEY_ROOMHOLDER + " TEXT NOT NULL, " +
KEY_ITEMWEIGHT + " TEXT NOT NULL, " +
KEY_ITEMMOVEHOLDER + " TEXT NOT NULL);";
public static final String CREATE_TABLE_4 = "CREATE TABLE " + DATABASE_TABLE4 + " (" +
KEY_ID4 + " INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_TODOMOVE + " TEXT NOT NULL, " +
KEY_CB1 + " TEXT NOT NULL, " +
KEY_CB2 + " TEXT NOT NULL, " +
KEY_CB3 + " TEXT NOT NULL, " +
KEY_CB4 + " TEXT NOT NULL, " +
KEY_CB5 + " TEXT NOT NULL, " +
KEY_CB6 + " TEXT NOT NULL, " +
KEY_CB7 + " TEXT NOT NULL, " +
KEY_CB8 + " TEXT NOT NULL, " +
KEY_CB9 + " TEXT NOT NULL, " +
KEY_CB10 + " TEXT NOT NULL, " +
KEY_CB11 + " TEXT NOT NULL, " +
KEY_CB12 + " TEXT NOT NULL, " +
KEY_CB13 + " TEXT NOT NULL, " +
KEY_CB14 + " TEXT NOT NULL, " +
KEY_CB15 + " TEXT NOT NULL, " +
KEY_CB16 + " TEXT NOT NULL, " +
KEY_CB17 + " TEXT NOT NULL, " +
KEY_CB18 + " TEXT NOT NULL, " +
KEY_CB19 + " TEXT NOT NULL, " +
KEY_CB20 + " TEXT NOT NULL, " +
KEY_CB21 + " TEXT NOT NULL, " +
KEY_CB22 + " TEXT NOT NULL, " +
KEY_CB23 + " TEXT NOT NULL, " +
KEY_CB24 + " TEXT NOT NULL, " +
KEY_CB25 + " TEXT NOT NULL, " +
KEY_CB26 + " TEXT NOT NULL, " +
KEY_CB27 + " TEXT NOT NULL, " +
KEY_CB28 + " TEXT NOT NULL, " +
KEY_CB29 + " TEXT NOT NULL, " +
KEY_CB30 + " TEXT NOT NULL, " +
KEY_CB31 + " TEXT NOT NULL, " +
KEY_CB32 + " TEXT NOT NULL, " +
KEY_CB33 + " TEXT NOT NULL, " +
KEY_CB34 + " TEXT NOT NULL, " +
KEY_CB35 + " TEXT NOT NULL, " +
KEY_CB36 + " TEXT NOT NULL, " +
KEY_CB37 + " TEXT NOT NULL, " +
KEY_CB38 + " TEXT NOT NULL);";
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE_1);
db.execSQL(CREATE_TABLE_2);
db.execSQL(CREATE_TABLE_3);
db.execSQL(CREATE_TABLE_4);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE1);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE2);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE3);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE4);
onCreate(db);
}
}
public SQLHandler(Context c){
ourContext = c;
}
public SQLHandler open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createMove(String smovename){
ContentValues cv = new ContentValues();
cv.put(KEY_MOVENAME, smovename);
cv.put(KEY_MOVEDATE, "Not yet set");
cv.put(KEY_TOTALMOVEWEIGHT, "0");
return ourDatabase.insert(DATABASE_TABLE1, null, cv);
}
public long setTodo(String smovename){
ContentValues cv = new ContentValues();
cv.put(KEY_TODOMOVE, smovename);
cv.put(KEY_CB1, "Not Done");
cv.put(KEY_CB2, "Not Done");
cv.put(KEY_CB3, "Not Done");
cv.put(KEY_CB4, "Not Done");
cv.put(KEY_CB5, "Not Done");
cv.put(KEY_CB6, "Not Done");
cv.put(KEY_CB7, "Not Done");
cv.put(KEY_CB8, "Not Done");
cv.put(KEY_CB9, "Not Done");
cv.put(KEY_CB10, "Not Done");
cv.put(KEY_CB11, "Not Done");
cv.put(KEY_CB12, "Not Done");
cv.put(KEY_CB13, "Not Done");
cv.put(KEY_CB14, "Not Done");
cv.put(KEY_CB15, "Not Done");
cv.put(KEY_CB16, "Not Done");
cv.put(KEY_CB17, "Not Done");
cv.put(KEY_CB18, "Not Done");
cv.put(KEY_CB19, "Not Done");
cv.put(KEY_CB20, "Not Done");
cv.put(KEY_CB21, "Not Done");
cv.put(KEY_CB22, "Not Done");
cv.put(KEY_CB23, "Not Done");
cv.put(KEY_CB24, "Not Done");
cv.put(KEY_CB25, "Not Done");
cv.put(KEY_CB26, "Not Done");
cv.put(KEY_CB27, "Not Done");
cv.put(KEY_CB28, "Not Done");
cv.put(KEY_CB29, "Not Done");
cv.put(KEY_CB30, "Not Done");
cv.put(KEY_CB31, "Not Done");
cv.put(KEY_CB32, "Not Done");
cv.put(KEY_CB33, "Not Done");
cv.put(KEY_CB34, "Not Done");
cv.put(KEY_CB35, "Not Done");
cv.put(KEY_CB36, "Not Done");
cv.put(KEY_CB37, "Not Done");
cv.put(KEY_CB38, "Not Done");
return ourDatabase.insert(DATABASE_TABLE4, null, cv);
}
public void setMoveDate(String smovedate, String smovename){
ContentValues cv = new ContentValues();
cv.put(KEY_MOVEDATE, smovedate);
ourDatabase.update(DATABASE_TABLE1, cv, KEY_MOVENAME + "='" + smovename + "'", null);
}
public void setRoomWeight(String sroomweight, String smovename, String sroomname){
ContentValues cv = new ContentValues();
cv.put(KEY_ROOMWEIGHT, sroomweight);
ourDatabase.update(DATABASE_TABLE2, cv, KEY_ROOMMOVEHOLDER + "='" + smovename + "'" + " AND " + KEY_ROOM + "='" + sroomname + "'", null);
}
public void updateTodo(String todoMove, int todoName, String todoValue){
ContentValues cv = new ContentValues();
String[] stodo = new String[]{KEY_CB1, KEY_CB2, KEY_CB3, KEY_CB4, KEY_CB5, KEY_CB6, KEY_CB7, KEY_CB8, KEY_CB9, KEY_CB10
, KEY_CB11, KEY_CB12, KEY_CB13, KEY_CB14, KEY_CB15, KEY_CB16, KEY_CB17, KEY_CB18, KEY_CB19, KEY_CB20
, KEY_CB21, KEY_CB22, KEY_CB23, KEY_CB24, KEY_CB25, KEY_CB26, KEY_CB27, KEY_CB28, KEY_CB29, KEY_CB30
, KEY_CB31, KEY_CB32, KEY_CB33, KEY_CB34, KEY_CB35, KEY_CB36, KEY_CB37, KEY_CB38};
cv.put(stodo[todoName], todoValue);
ourDatabase.update(DATABASE_TABLE4, cv, KEY_TODOMOVE + "='" + todoMove + "'", null);
}
public long todoList(String todoitem){
ContentValues cv = new ContentValues();
cv.put(todoitem, "Done");
return ourDatabase.insert(DATABASE_TABLE4, null, cv);
}
public Cursor getMove(){
String[] columns = new String[]{KEY_ID1, KEY_MOVENAME};
Cursor cursor = ourDatabase.query(DATABASE_TABLE1, columns, null, null, null, null, null);
return cursor;
}
public String getTodo(String m, int tn){
String[] columns = new String[]{KEY_ID4, KEY_TODOMOVE, KEY_CB1, KEY_CB2, KEY_CB3, KEY_CB4, KEY_CB5, KEY_CB6, KEY_CB7, KEY_CB8, KEY_CB9, KEY_CB10
, KEY_CB11, KEY_CB12, KEY_CB13, KEY_CB14, KEY_CB15, KEY_CB16, KEY_CB17, KEY_CB18, KEY_CB19, KEY_CB20
, KEY_CB21, KEY_CB22, KEY_CB23, KEY_CB24, KEY_CB25, KEY_CB26, KEY_CB27, KEY_CB28, KEY_CB29, KEY_CB30
, KEY_CB31, KEY_CB32, KEY_CB33, KEY_CB34, KEY_CB35, KEY_CB36, KEY_CB37, KEY_CB38};
Cursor c = ourDatabase.query(DATABASE_TABLE4, columns, KEY_TODOMOVE + "= '" + m + "'", null, null, null, null);
if (c != null) {
c.moveToFirst();
String toDo = c.getString(tn);
return toDo;
}
return null;
}
}