使用从com.activeandroid.app.Application继承的类时,应用程序崩溃

时间:2015-01-29 07:04:44

标签: android activeandroid

我使用我想要创建类的我的库Projects ActiveAndroid:

import com.activeandroid.app.Application;
public class App extends Application {
    @Override
    public void onCreate()
    {
        super.onCreate();
    }
}

在模拟器上一切正常,数据存储在数据库中等等。但是当你启动手机时,应用程序崩溃了。并立即。

错误:

01-29 12:55:19.027  14098-14098/com.skip.client.customer E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.skip.client.customer, PID: 14098
    java.lang.NoClassDefFoundError: android.support.v4.app.ActivityCompat21$SharedElementCallbackImpl
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:305)
            at com.activeandroid.ReflectionUtils.getModelClasses(ReflectionUtils.java:83)
            at com.activeandroid.DatabaseHelper.onCreate(DatabaseHelper.java:46)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
            at com.activeandroid.Registry.openDatabase(Registry.java:149)
            at com.activeandroid.Registry.initialize(Registry.java:107)
            at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:8)
            at com.activeandroid.app.Application.onCreate(Application.java:9)
            at com.skip.client.customer.App.onCreate(App.java:12)
            at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4473)
            at android.app.ActivityThread.access$1500(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)

1 个答案:

答案 0 :(得分:0)

It looks like getModelClasses() in active android is trying to find a java class in your code that matches the model classes you would have specified in your AndroidManifest.xml. Can you please post your manifest? Specifically, I'm looking for a line like this:

    <meta-data
        android:name="AA_MODELS"
        android:value="
            com.example.myapp.Book,
            com.example.myapp.Chapter,
            com.example.myapp.Library
        "/>

If classes Book, Chapter, and Library are not defined in your code, then you'd get the no-class-def-found exception.

Also, I'd recommend changing your app to have your custom app class derive from android.app.Application instead of from the ActiveAndroid app base class. And then explicitly initialize ActiveAndroid. This will allow you to have your own exception handling around ActiveAndroid.initialize() and then explore why your exception is being thrown. You can also use the debugger to step into the initialize() method and see where the exception is coming from.

import com.activeandroid.ActiveAndroid;
public class App extends android.app.Application {
    @Override
    public void onCreate()
    {
        super.onCreate();
        // add your own exception handling around this.  Not that you can do much
        // in your app without a database, but at least you'd be able to log a
        // meaningful error to the log so you can know details about the exception
        // being thrown.
        ActiveAndroid.initialize(this);
    }
}