Reputation: 125
I've got a Android.App.Application which tries to create a DB. I'm using the SQLite db example code. It works great if I use it inside an Activity. When I try to create the exact same thing inside my Android.App.Application it crashes. I've got no clue why.
When I'm calling this from an activity:
public class Manager extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
fbdbhelper = new FishingBuddyOpenHelper(getApplicationContext());
db = fbdbhelper.getTbfw().getReadableDatabase();
}
A sqlite db is created (/data/data/com.fishingbuddy/databases/)
When done from an Android.App.Application it fails:
public class FishingManager extends Application{
public FishingManager() {
fbdbhelper = new FishingBuddyOpenHelper(getApplicationContext());
db = fbdbhelper.getTbfw().getReadableDatabase();
}
09-22 21:19:18.386: E/AndroidRuntime(21881): FATAL EXCEPTION: main 09-22 21:19:18.386: E/AndroidRuntime(21881): java.lang.RuntimeException: Unable to >instantiate application com.fishingbuddy.logic.FishingManager: java.lang.NullPointerException 09-22 21:19:18.386: E/AndroidRuntime(21881): at android.app.LoadedApk.makeApplication(LoadedApk.java:504) 09-22 21:19:18.386: E/AndroidRuntime(21881): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4550) 09-22 21:19:18.386: E/AndroidRuntime(21881): at android.app.ActivityThread.access$1300(ActivityThread.java:153) 09-22 21:19:18.386: E/AndroidRuntime(21881): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307) 09-22 21:19:18.386: E/AndroidRuntime(21881): at android.os.Handler.dispatchMessage(Handler.java:99) 09-22 21:19:18.386: E/AndroidRuntime(21881): at android.os.Looper.loop(Looper.java:137) 09-22 21:19:18.386: E/AndroidRuntime(21881): at android.app.ActivityThread.main(ActivityThread.java:5227) 09-22 21:19:18.386: E/AndroidRuntime(21881): at java.lang.reflect.Method.invokeNative(Native Method) 09-22 21:19:18.386: E/AndroidRuntime(21881): at java.lang.reflect.Method.invoke(Method.java:511) 09-22 21:19:18.386: E/AndroidRuntime(21881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 09-22 21:19:18.386: E/AndroidRuntime(21881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 09-22 21:19:18.386: E/AndroidRuntime(21881): at dalvik.system.NativeStart.main(Native Method) 09-22 21:19:18.386: E/AndroidRuntime(21881): Caused by: java.lang.NullPointerException 09-22 21:19:18.386: E/AndroidRuntime(21881): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109) 09-22 21:19:18.386: E/AndroidRuntime(21881): at com.fishingbuddy.logic.FishingManager.(FishingManager.java:26) 09-22 21:19:18.386: E/AndroidRuntime(21881): at java.lang.Class.newInstanceImpl(Native Method) 09-22 21:19:18.386: E/AndroidRuntime(21881): at java.lang.Class.newInstance(Class.java:1319) 09-22 21:19:18.386: E/AndroidRuntime(21881): at android.app.Instrumentation.newApplication(Instrumentation.java:983) 09-22 21:19:18.386: E/AndroidRuntime(21881): at android.app.Instrumentation.newApplication(Instrumentation.java:968) 09-22 21:19:18.386: E/AndroidRuntime(21881): at android.app.LoadedApk.makeApplication(LoadedApk.java:499) 09-22 21:19:18.386: E/AndroidRuntime(21881): ... 11 more
I'm using a Application because I'm using the singleton pattern for my FishingManager. It all worked great until I started the final part, saving all my data to an SQLite db.
Thanks in advance
Upvotes: 1
Views: 98
Reputation: 6978
You probably need to declare a content provider in the manifest file like given in this example
Upvotes: 0
Reputation: 1006819
When I'm calling this from an activity:
It will crash with a SuperNotCalledException
.
When done from an Android.App.Application it fails
Even if you deleted the two lines of code here, it will crash, with a SuperNotCalledException
.
Please call super.onCreate()
in onCreate()
of an Activity
(or Application
) before doing much of anything else.
I'm using a Application because I'm using the singleton pattern for my FishingManager.
You do not need an Application
subclass to have a singleton SQLiteOpenHelper
. You just need a singleton SQLiteOpenHelper
that you lazy-initialize on first use.
Upvotes: 1