I do have several apps that are designed the same way. An extended Application holds the database connection and an extended SQLiteOpenHelper has all the code.
This works when used in a standard android project.
It crashes when the extended Application is in a Library Project.
The following code is the extended Application class. It is located in the Library Project. It crashes if it is used in a Library Project but not if it is part of a standard Android app without a Library Project. I tried to move the MyApplication class from the Library Project into the Project (changing the name in the Manifest) - without success.
My guess, but I'm simply fishing here, is that MySQLiteOpenHelper uses the rights/permissions/location of the library project and not of the project itself.
public class MyApplication extends Application {
private static SQLiteDatabase sqliteDatabase;
private static MySQLiteOpenHelper sqliteOpenHelper;
public static SQLiteDatabase getSqliteDatabase() {
return sqliteDatabase;
}
public static MySQLiteOpenHelper getSqliteOpenHelper() {
return sqliteOpenHelper;
}
@Override
public void onCreate() {
super.onCreate();
MyPreferenceActivity.createSettings(getApplicationContext());
sqliteOpenHelper = new MySQLiteOpenHelper(getApplicationContext());
if (sqliteOpenHelper != null) {
sqliteDatabase = sqliteOpenHelper.getWritableDatabase();
}
}
}
This is part of the extended SQLiteOpenHelper class:
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
private static final int DATABASEVERSION = 18;
private static final String DATABASENAME = "myproduct.db";
private Context context;
private SQLiteDatabase sqliteDatabase;
private String location;
public MySQLiteOpenHelper(final Context context) {
super(context, DATABASENAME, null, DATABASEVERSION);
this.context = context;
}
@Override
public SQLiteDatabase getWritableDatabase() {
...
try {
if (Tools.isSDCardWriteable() && (fileSdDbDir.exists() || fileSdDb.exists())) {
// Database on SD-card works perfect
// If I use this to create the internal database it fails here too
location = sdDb;
sqliteDatabase = SQLiteDatabase.openOrCreateDatabase(location, null);
int version = sqliteDatabase.getVersion();
if (version != DATABASEVERSION) {
sqliteDatabase.beginTransaction();
try {
if (version == 0) {
onCreate(sqliteDatabase);
} else {
onUpgrade(sqliteDatabase, version, DATABASEVERSION);
}
sqliteDatabase.setVersion(DATABASEVERSION);
sqliteDatabase.setTransactionSuccessful();
} finally {
sqliteDatabase.endTransaction();
}
}
onOpen(sqliteDatabase);
} else {
// internal database fails to create
location = intDb;
sqliteDatabase = super.getWritableDatabase(); <-- fails
}
} catch (Exception exception) {
if (MyPreferenceActivity.DEBUG) Log.d("Exception", exception.getMessage()); <-- crashes
}
return sqliteDatabase;
}
@Override
public void onCreate(final SQLiteDatabase sqliteDatabase) {
//
}
@Override
public void onUpgrade(final SQLiteDatabase sqliteDatabase, final int oldVersion, final int newVersion) {
//
}
...
}
This is the Manifest of the Library Project:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.yyy.zzzlib.android" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="11" />
</manifest>
This is part of the projects Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="preferExternal"
android:versionCode="41"
android:versionName="5.0.3"
package="xxx.yyy.zzz.android" >
<application
...
android:name="xxx.yyy.zzzlib.android.MyApplication" >
...
</application>
...
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="11" />
</manifest>
I don't have any idea why this doesn't work. Perhaps a Context thing but the error points to the file. I don't have any clues.
Is there something wrong with this approach? It's working perfect in a standard Android project environment.
Many thanks in advance.
EDIT:
Stacktrace shows crash in Log.d but it's the database call that's failing:
11-04 17:53:56.143: E/AndroidRuntime(1127): FATAL EXCEPTION: main
11-04 17:53:56.143: E/AndroidRuntime(1127): java.lang.RuntimeException: Unable to create application xxx.yyy.zzzlib.android.MyApplication: java.lang.NullPointerException: println needs a message
11-04 17:53:56.143: E/AndroidRuntime(1127): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3275)
11-04 17:53:56.143: E/AndroidRuntime(1127): at android.app.ActivityThread.access$2200(ActivityThread.java:117)
11-04 17:53:56.143: E/AndroidRuntime(1127): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
11-04 17:53:56.143: E/AndroidRuntime(1127): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 17:53:56.143: E/AndroidRuntime(1127): at android.os.Looper.loop(Looper.java:130)
11-04 17:53:56.143: E/AndroidRuntime(1127): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-04 17:53:56.143: E/AndroidRuntime(1127): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 17:53:56.143: E/AndroidRuntime(1127): at java.lang.reflect.Method.invoke(Method.java:507)
11-04 17:53:56.143: E/AndroidRuntime(1127): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-04 17:53:56.143: E/AndroidRuntime(1127): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-04 17:53:56.143: E/AndroidRuntime(1127): at dalvik.system.NativeStart.main(Native Method)
11-04 17:53:56.143: E/AndroidRuntime(1127): Caused by: java.lang.NullPointerException: println needs a message
11-04 17:53:56.143: E/AndroidRuntime(1127): at android.util.Log.println_native(Native Method)
11-04 17:53:56.143: E/AndroidRuntime(1127): at android.util.Log.d(Log.java:137)
11-04 17:53:56.143: E/AndroidRuntime(1127): at xxx.yyy.zzzlib.android.MySQLiteOpenHelper.getWritableDatabase(MySQLiteOpenHelper.java:1493)
11-04 17:53:56.143: E/AndroidRuntime(1127): at xxx.yyy.zzzlib.android.MyApplication.onCreate(MyApplication.java:27)
11-04 17:53:56.143: E/AndroidRuntime(1127): at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
11-04 17:53:56.143: E/AndroidRuntime(1127): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3272)