我有一个Android服务,它在设备启动时被激活,它在应用程序处于活动状态时正常工作或被注销但是如果我强制停止应用程序它会抛出异常,该服务正在尝试访问数据库和抛出sqllite数据库锁定异常。错误日志如下所示.DBHandler是用于进行数据库事务的类,NotificationService是服务类。
getConfigData()方法需要在应用程序关闭时它变为null的上下文。有没有办法解决这个问题?
08-28 11:48:05.897: E/AndroidRuntime(5839): FATAL EXCEPTION: main
08-28 11:48:05.897: E/AndroidRuntime(5839): java.lang.RuntimeException: Unable to start service com.cwep.sync.NotificationService@414b4690 with null: java.lang.StringIndexOutOfBoundsException: length=0; index=0
08-28 11:48:05.897: E/AndroidRuntime(5839): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2655)
08-28 11:48:05.897: E/AndroidRuntime(5839): at android.app.ActivityThread.access$1900(ActivityThread.java:149)
08-28 11:48:05.897: E/AndroidRuntime(5839): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
08-28 11:48:05.897: E/AndroidRuntime(5839): at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 11:48:05.897: E/AndroidRuntime(5839): at android.os.Looper.loop(Looper.java:153)
08-28 11:48:05.897: E/AndroidRuntime(5839): at android.app.ActivityThread.main(ActivityThread.java:4987)
08-28 11:48:05.897: E/AndroidRuntime(5839): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 11:48:05.897: E/AndroidRuntime(5839): at java.lang.reflect.Method.invoke(Method.java:511)
08-28 11:48:05.897: E/AndroidRuntime(5839): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-28 11:48:05.897: E/AndroidRuntime(5839): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-28 11:48:05.897: E/AndroidRuntime(5839): at dalvik.system.NativeStart.main(Native Method)
08-28 11:48:05.897: E/AndroidRuntime(5839): Caused by: java.lang.StringIndexOutOfBoundsException: length=0; index=0
08-28 11:48:05.897: E/AndroidRuntime(5839): at android.app.ContextImpl.validateFilePath(ContextImpl.java:1705)
08-28 11:48:05.897: E/AndroidRuntime(5839): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:855)
08-28 11:48:05.897: E/AndroidRuntime(5839): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
08-28 11:48:05.897: E/AndroidRuntime(5839): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
08-28 11:48:05.897: E/AndroidRuntime(5839): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
08-28 11:48:05.897: E/AndroidRuntime(5839): at com.cwep.dbhandler.DBHandler.getWritableDatabase(DBHandler.java:2756)
08-28 11:48:05.897: E/AndroidRuntime(5839): at com.cwep.dbhandler.DBHandler.getConfigData(DBHandler.java:488)
08-28 11:48:05.897: E/AndroidRuntime(5839): at com.cwep.sync.NotificationService.onStartCommand(NotificationService.java:58)
08-28 11:48:05.897: E/AndroidRuntime(5839): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2638)
08-28 11:48:05.897: E/AndroidRuntime(5839): ... 10 more
答案 0 :(得分:1)
看起来您将空字符串""
作为数据库名称传递给SQLiteOpenHelper
构造函数。您需要传入一个非空名称,该名称对于内存数据库是有效的,或者是null
(不是空字符串)。