12-31 10:58:28.025: E/AndroidRuntime(16322): FATAL EXCEPTION: Timer-0
12-31 10:58:28.025: E/AndroidRuntime(16322): java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
12-31 10:58:28.025: E/AndroidRuntime(16322): at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:1031)
12-31 10:58:28.025: E/AndroidRuntime(16322): at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:746)
12-31 10:58:28.025: E/AndroidRuntime(16322): at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:400)
12-31 10:58:28.025: E/AndroidRuntime(16322): at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:905)
12-31 10:58:28.025: E/AndroidRuntime(16322): at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:834)
12-31 10:58:28.025: E/AndroidRuntime(16322): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
12-31 10:58:28.025: E/AndroidRuntime(16322): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:144)
12-31 10:58:28.025: E/AndroidRuntime(16322): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
12-31 10:58:28.025: E/AndroidRuntime(16322): at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:197)
12-31 10:58:28.025: E/AndroidRuntime(16322): at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:237)
12-31 10:58:28.025: E/AndroidRuntime(16322): at com.eyepax.horseapp.data.dbadapters.ContactGroupDbAdapter.getContactGroups(ContactGroupDbAdapter.java:138)
12-31 10:58:28.025: E/AndroidRuntime(16322): at com.eyepax.horseapp.data.DataManager.getGroups(DataManager.java:1548)
12-31 10:58:28.025: E/AndroidRuntime(16322): at com.eyepax.horseapp.sync.ContactSync.contactGroupSync(ContactSync.java:43)
12-31 10:58:28.025: E/AndroidRuntime(16322): at com.eyepax.horseapp.service.SynchingService.callSync(SynchingService.java:77)
12-31 10:58:28.025: E/AndroidRuntime(16322): at com.eyepax.horseapp.service.SynchingService.access$0(SynchingService.java:70)
12-31 10:58:28.025: E/AndroidRuntime(16322): at com.eyepax.horseapp.service.SynchingService$1.run(SynchingService.java:57)
/**
* Returns ContactGroup.
*
* @return ArrayList<ContactGroup>
*/
public ArrayList<ContactGroup> getContactGroups() {
this.open();
Cursor cursor = database.rawQuery("SELECT * FROM " + TABLE_NAME, null);
ArrayList<ContactGroup> list = null;
if (cursor != null && cursor.moveToFirst()) {
list = new ArrayList<ContactGroup>();
do {
try {
ContactGroup contactGroup = new ContactGroup();
contactGroup.setGroupId(cursor.getString(cursor.getColumnIndex(GROUP_ID)));
contactGroup.setGroupName(cursor.getString(cursor.getColumnIndex(GROUP_NAME)));
contactGroup.setIsDeleted(cursor.getInt(cursor.getColumnIndex(GROUP_IS_DELETED)));
contactGroup.setUpdatedDate(cursor.getString(cursor.getColumnIndex(GROUP_UPDATE_DATE)));
contactGroup.setIsSync(cursor.getInt(cursor.getColumnIndex(GROUP_IS_SYNC)));
list.add(contactGroup);
} catch (Exception e) {
}
} while (cursor.moveToNext());
}
if (cursor != null)
cursor.close();
return list;
}
public class BaseDbAdapter {
/**
* Application context.
*/
protected Context context;
/**
* Database.
*/
protected SQLiteDatabase database;
/**
* Database helper class.
*/
protected SQLiteDataBaseHelper dbHelper;
/**
* StringBuilder
*/
protected StringBuilder stringBuilder;
public static final String CREATE_TABLE_PRE_TEXT = "create table if not exists ";
/**
* Returns StringBuilder object.
*
* @return
*/
protected StringBuilder getStringBuilder() {
if (stringBuilder == null) {
stringBuilder = new StringBuilder();
}
return stringBuilder;
}
/**
* Opens a connection to the database.
*
* @throws SQLException
*/
public synchronized void open() throws SQLException {
dbHelper = SQLiteDataBaseHelper.getInstance(context);
database = dbHelper.getWritableDatabase();
}
/**
* Closes the connection.
*/
public synchronized void close() {
database.close();
}
public static String getDeleteTableStatement(String tableName) {
return "DELETE TABLE " + tableName;
}
我使用了SQLiteDataBaseHelper类,其getInstance方法更改为synchronize.
作为此答案,但仍然存在问题not fixed。
/**
* Get an instance of SQLiteDataBaseHelper.
*
* @param context
* @return SQLiteDataBaseHelper
*/
public static synchronized SQLiteDataBaseHelper getInstance(Context context) {
// if(dbName == null) {
// dbName = DataManager.getSQLiteDatabaseName(context);
// }
if (instance == null) {
instance = new SQLiteDataBaseHelper(context);
}
return instance;
}
我认为问题出在同步
上。当多个线程访问时,会对数据库的状态产生误解。
您应该避免将数据库设置为实例变量,而应将其设置为帮助器类(最好是扩展SQLiteOpenHelper的类)中的本地方法。下面展示了获取数据库并对其进行操作的示例:
public void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Contact Name
values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone Number
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
请查看本教程:Android SQLite数据库教程
public class DatabaseHelper extends SQLiteOpenHelper {
......
// no need to synchronize
public Cursor getBookList(){
SQLiteDatabase db = getReadableDatabase(); // or getWritableDatabase()
Cursor c = db.query(BookEntry.TABLE_NAME, null, null, null, null, null, BookEntry.COLUMN_NAME + " ASC");
return c;
}
// no need to override close method, because that one is very well coded already
// SQLiteOpenHelper will close any opened database, with its service close method.
}
public class SomeAdapter{
// no need to synchronize, as all DatabaseHelper instances are method local
public int getBookCount(){
DatabaseHelper dbHelper = new DatabaseHelper(context);
Cursor c = dbHelper.getBookList();
int rowCount = c.getCount();
c.close(); // close the cursor once used.
dbHelper.close(); // ask the helper to close any open database.
return rowCount;
}
}
我不明白为什么如果我之前调用,错误会说数据库是关闭的。请支持!谢谢:)
我试着用谷歌搜索了很多个小时,但没有结果。请帮我把它修好。任何帮助都将不胜感激。谢谢!
我已经在StackOverflow和web上搜索了这个问题的答案,但我找不到答案。当我在姜饼上运行我的应用程序时,它运行得很好。但是当我在4.2.2上运行它时,我会得到这样的错误:我有一个包含两个片段的SherlockFragmentActivity。 片段1: SherlockFragmentActivity: 我已经被困在这个问题上几个小时了(没有笑话),真的很感激任何帮助。再次感谢。
问题内容: 我正在阅读有关此问题,但仍然没有找到解决方案。我注意到有时,我的应用会引发此错误: 我有一个使用此方法的文件,以获取其实例: 然后,我有类似这样的方法(它在发生该错误的行中崩溃)。它几乎从不崩溃,但有时会崩溃。 就这样,在我使用的所有方法中: 然后关闭游标和数据库。在这种情况下,该错误被抛出该行: 我不明白为什么如果我之前打电话,错误提示数据库已关闭。请支持!谢谢 :) 问题答案: 去
null java.lang.IllegalStateException:在onSaveInstanceState e/androidRuntime(9008):at android.support.v4.app.fragmentManagerImpl.checkStateLoss(fragmentManager.java:1354)e/androidRuntime(9008):at androi