此问题是收集别人遇到的:
原文见: http://bbs.csdn.net/topics/390286454?page=1#post-392965984
23 14:24:18.853 E/SQLiteDatabase(14233): Failed to open database '/data/data/com.example.androidproject51rc/databases/dictionary.db'.
10-23 14:24:18.853 E/SQLiteDatabase(14233): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:627)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:313)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:287)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:709)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at com.example.androidproject51rc.base.DBManager.openDatabase(DBManager.java:49)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at com.example.androidproject51rc.base.DBManager.GetSearchHistory(DBManager.java:88)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at com.example.androidproject51rc.JobSearchActivity.LoadDefaultInfo(JobSearchActivity.java:89)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at com.example.androidproject51rc.JobSearchActivity.onCreate(JobSearchActivity.java:65)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.app.Activity.performCreate(Activity.java:5008)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.os.Looper.loop(Looper.java:137)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-23 14:24:18.853 E/SQLiteDatabase(14233): at dalvik.system.NativeStart.main(Native Method)
10-23 14:24:18.853 E/Exception(14233): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
10-23 14:24:18.853 D/AndroidRuntime(14233): Shutting down VM
10-23 14:24:18.853 W/dalvikvm(14233): threadid=1: thread exiting with uncaught exception (group=0x40d53300)
10-23 14:24:18.853 E/AndroidRuntime(14233): FATAL EXCEPTION: main
10-23 14:24:18.853 E/AndroidRuntime(14233): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidproject51rc/com.example.androidproject51rc.JobSearchActivity}: java.lang.NullPointerException
10-23 14:24:18.853 E/AndroidRuntime(14233): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-23 14:24:18.853 E/AndroidRuntime(14233): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-23 14:24:18.853 E/AndroidRuntime(14233): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-23 14:24:18.853 E/AndroidRuntime(14233): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-23 14:24:18.853 E/AndroidRuntime(14233): at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 14:24:18.853 E/AndroidRuntime(14233): at android.os.Looper.loop(Looper.java:137)
10-23 14:24:18.853 E/AndroidRuntime(14233): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-23 14:24:18.853 E/AndroidRuntime(14233): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 14:24:18.853 E/AndroidRuntime(14233): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 14:24:18.853 E/AndroidRuntime(14233): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-23 14:24:18.853 E/AndroidRuntime(14233): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-23 14:24:18.853 E/AndroidRuntime(14233): at dalvik.system.NativeStart.main(Native Method)
10-23 14:24:18.853 E/AndroidRuntime(14233): Caused by: java.lang.NullPointerException
10-23 14:24:18.853 E/AndroidRuntime(14233): at com.example.androidproject51rc.base.DBManager.GetSearchHistory(DBManager.java:89)
10-23 14:24:18.853 E/AndroidRuntime(14233): at com.example.androidproject51rc.JobSearchActivity.LoadDefaultInfo(JobSearchActivity.java:89)
10-23 14:24:18.853 E/AndroidRuntime(14233): at com.example.androidproject51rc.JobSearchActivity.onCreate(JobSearchActivity.java:65)
10-23 14:24:18.853 E/AndroidRuntime(14233): at android.app.Activity.performCreate(Activity.java:5008)
10-23 14:24:18.853 E/AndroidRuntime(14233): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-23 14:24:18.853 E/AndroidRuntime(14233): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-23 14:24:18.853 E/AndroidRuntime(14233): ... 11 more
解决方法:
问题已经解决了,直接贴源码把,原先的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//获取行业列表
public
ArrayList<Industry> GetIndustryList(){
ArrayList<Industry> IndustryList=
new
ArrayList<Industry>();
SQLiteDatabase db=openDatabase();
db.beginTransaction();
Cursor cursor = db.rawQuery(
"select * from dcIndustry"
,
null
);
while
(cursor.moveToNext()){
IndustryList.add(
new
Industry(cursor.getString(
0
),cursor.getString(
1
)));
}
db.close();
return
IndustryList;
}
|
改正其实很简单,在db.close()前加db.endTransaction();就可以了
修正后:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//获取行业列表
public
ArrayList<Industry> GetIndustryList(){
ArrayList<Industry> IndustryList=
new
ArrayList<Industry>();
SQLiteDatabase db=openDatabase();
db.beginTransaction();
Cursor cursor = db.rawQuery(
"select * from dcIndustry"
,
null
);
while
(cursor.moveToNext()){
IndustryList.add(
new
Industry(cursor.getString(
0
),cursor.getString(
1
)));
}
db.endTransaction();
db.close();
return
IndustryList;
}
|
解决方法是在国外的某论坛找到的 原因是4.0以前的版本db.close();会结束事务,而Jelly Bean 以后的版本因为安全性的问题,必须结束即endTransactiony以后才能再次访问本地数据库。哎。。苦逼的百度啊。。另外,吐槽一下不向下兼容的Android
详细见:http://bbs.csdn.net/topics/390286454?page=1#post-392965984