开发者

Application force close when trying to close SQLite database

I have a superclass for my Activities (WhereWolfActivity) in my Android app that has a static SQLiteDatabaseOpenHelper for all subclasses to use. The database connection is opened in onCreate and closed in onDestroy (I keep track of the number of subclass Activities open to ensure the database connection is only closed when the last Activity in the app is destroyed). When a user logs 开发者_开发技巧out, they are taken to a welcome screen Activity that does not subclass WhereWolfActivity, so the database connection is closed. However, the application Force Closes when the application logs out and I get the following logcat output:

08-11 02:28:24.858: ERROR/AndroidRuntime(29022): FATAL EXCEPTION: main
08-11 02:28:24.858: ERROR/AndroidRuntime(29022): java.lang.RuntimeException: Unable to destroy activity {uk.ac.ic.doc.vmw10.wherewolf/uk.ac.ic.doc.vmw10.wherewolf.activities.Tabs}: java.lang.NullPointerException
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3655)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at android.app.ActivityThread.access$2900(ActivityThread.java:125)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at android.os.Looper.loop(Looper.java:123)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at java.lang.reflect.Method.invokeNative(Native Method)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at java.lang.reflect.Method.invoke(Method.java:521)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at dalvik.system.NativeStart.main(Native Method)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022): Caused by: java.lang.NullPointerException
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at uk.ac.ic.doc.vmw10.wherewolf.activities.WhereWolfActivity.onDestroy(WhereWolfActivity.java:119)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642)
08-11 02:28:24.858: ERROR/AndroidRuntime(29022):     ... 11 more

The NullPointerException occurs on the line that says: dbHelper.close();

Any idea why attempting to close the database would give a null pointer exception?


We can't really know why without seeing your code, but apparently your dbHelper is null. This has nothing to do with trying to close it.

On another note, make your life simpler and have a singleton class for your database. As for closing, you don't really need to do this, just keep it open. When your process dies, that will take care of closing the database. Make sure you close all your cursors though.

Something like:

public class MyDbHelper extends SQLiteOpenHelper {

    private Context context;

    private static MyDbHelper instance;

    public static MyDbHelper getInstance(Context context) {
        if (instance == null) {
            instance = new MyDbHelper(context);
        }

        return instance;
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜