无法读取数据库 - NullPointerException

时间:2014-07-29 14:21:43

标签: java android

这应该很容易,但我不能创建OrReadDatabase而不会抛出异常

 public class DatabaseStuff extends ListActivity {
    private SQLiteDatabase _db;

    public DatabaseStuff() {
        _db = openOrCreateDatabase("Db", MODE_PRIVATE, null); 'eeeeek
    }

它会引发异常。我对Java完全不熟悉,Logcat似乎对我说太多了解

java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3835)
            at android.view.View.performClick(View.java:4450)
            at android.view.View$PerformClick.run(View.java:18600)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5026)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3830)
            at android.view.View.performClick(View.java:4450)
            at android.view.View$PerformClick.run(View.java:18600)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5026)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:250)
            at com.lmsites.dave.lifecrymailinglist.MyActivity$DatabaseStuff.<init>(MyActivity.java:109)
            at com.lmsites.dave.lifecrymailinglist.MyActivity.SaveClick(MyActivity.java:71)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3830)
            at android.view.View.performClick(View.java:4450)
            at android.view.View$PerformClick.run(View.java:18600)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5026)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
            at dalvik.system.NativeStart.main(Native Method)

我需要指出的是,当我将方法移动到MyActivity类时,它可以按需运行。当我将函数移动到它自己的类中时会出现问题。

有人可以帮助我理解为什么我不能创建这个对象吗?

1 个答案:

答案 0 :(得分:1)

您的问题是,您尝试以错误的方式创建Activity(您的类扩展ListActivity)。您不应该明确地调用Activity的构造函数。

Android中的Activity具有生命周期。它由startActivity方法启动,该方法调用onCreate等生命周期方法来初始化Activity

当您在不调用这些方法的情况下创建Activity实例时,它未正确初始化,这会导致openOrCreateDatabase等方法失败。