ViewModel和LiveData导入导致NPE

时间:2019-03-15 12:55:50

标签: android android-gradle android-livedata

我刚刚将ViewModel和LiveData添加到了build.gradle中,这导致崩溃。需要明确的是,这是在没有向导入中添加任何新代码的情况下发生的。

由于没有要粘贴的代码,因此我将包括gradle依赖项和logcat->

依赖项:

dependencies {
    api 'androidx.annotation:annotation:1.0.1'
    api 'androidx.recyclerview:recyclerview:1.0.0'
    api 'androidx.appcompat:appcompat:1.0.2'
    api 'com.google.android.material:material:1.0.0'
    api 'androidx.browser:browser:1.0.0'
    api 'androidx.exifinterface:exifinterface:1.0.0'
    api 'androidx.vectordrawable:vectordrawable-animated:1.0.0'
    api 'androidx.multidex:multidex:2.0.1'
    api 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
    api "com.google.android.gms:play-services-base:16.1.0"
    api "com.google.firebase:firebase-appindexing:17.1.0"
    api "com.google.firebase:firebase-messaging:17.3.4"
    api files('src/main/libs/adobeMobileLibrary-4.17.2.jar')
    api files('src/main/libs/YouTubeAndroidPlayerApi.jar')
    api project(':PhotoView')
    api 'com.my.project:assets:2.4.0.34@aar'
    api 'com.chaoticmoon.chaosengine:volley-gson-networking:1.1.3@aar'
    api 'com.chaoticmoon.chaosengine:utils:0.2.3@aar'
    api 'org.apache.commons:commons-lang3:3.8.1'
    api 'com.squareup.retrofit2:retrofit:2.5.0'
    api 'com.squareup.retrofit2:converter-gson:2.5.0'
    api 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
    api 'com.squareup.okhttp3:logging-interceptor:3.11.0'
    api 'com.squareup.okhttp3:okhttp:3.12.0'
    api 'io.reactivex.rxjava2:rxandroid:2.1.0'
    api 'io.reactivex.rxjava2:rxjava:2.2.2'
    api 'com.squareup.picasso:picasso:2.71828'

    def lifecycle_version = "2.0.0"

    // ViewModel and LiveData
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

    api 'com.jakewharton:butterknife:10.0.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'

    api 'com.android.volley:volley:1.1.0'
    api 'se.emilsjolander:stickylistheaders:2.1.0'
    api 'com.newrelic.agent.android:android-agent:5.22.0'
    api 'com.facebook.stetho:stetho:1.5.0'

    api 'com.android.billingclient:billing:1.1'
    api 'com.google.android.gms:play-services-ads-identifier:16.0.0'
    api 'com.android.installreferrer:installreferrer:1.0'
    api 'com.tealium:library:5.5.4'
    api 'com.kochava.base:tracker:3.5.0'

    implementation 'androidx.core:core-ktx:1.0.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    testImplementation "junit:junit:4.12"
    androidTestImplementation 'androidx.multidex:multidex-instrumentation:2.0.0'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test:rules:1.1.1'
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    androidTestImplementation 'androidx.annotation:annotation:1.0.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

日志:

2019-03-15 08:46:52.870 23474-23474/com.my.poroject E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.my.poroject, PID: 23474
    java.lang.RuntimeException: Unable to get provider androidx.lifecycle.ProcessLifecycleOwnerInitializer: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Application.registerActivityLifecycleCallbacks(android.app.Application$ActivityLifecycleCallbacks)' on a null object reference
        at android.app.ActivityThread.installProvider(ActivityThread.java:6242)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722)
        at android.app.ActivityThread.-wrap1(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Application.registerActivityLifecycleCallbacks(android.app.Application$ActivityLifecycleCallbacks)' on a null object reference
        at androidx.lifecycle.LifecycleDispatcher.init(LifecycleDispatcher.java:44)
        at androidx.lifecycle.ProcessLifecycleOwnerInitializer.onCreate(ProcessLifecycleOwnerInitializer.java:36)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1919)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1894)
        at android.app.ActivityThread.installProvider(ActivityThread.java:6239)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722) 
        at android.app.ActivityThread.-wrap1(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

0 个答案:

没有答案