【问题标题】:No super method getLifecycle() after migrating to androidx迁移到androidx后没有超级方法getLifecycle()
【发布时间】:2018-11-09 16:02:46
【问题描述】:

我一直在尝试迁移我的应用程序以使用 androidx,但我似乎遇到了一个奇怪的错误。从我调用 getLifeCycle() 时扩展 AppCompatActivity 的活动中,它会引发以下异常

 Caused by: java.lang.NoSuchMethodError: No super method getLifecycle()Landroidx/lifecycle/Lifecycle; in class Landroidx/core/app/ComponentActivity; or its super classes 
    at androidx.fragment.app.FragmentActivity.getLifecycle(FragmentActivity.java:324)

我相信 AppCompatActivity 应该实现 LifecycleOwner 但事实并非如此。难道我做错了什么? 这是我的 gradle 依赖项

implementation files("libs/jsoup-1.8.3.jar")
implementation "com.github.philjay:MPAndroidChart:v3.0.2"
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.1.0-alpha01'
implementation "androidx.constraintlayout:constraintlayout:2.0.0-alpha2"
implementation 'androidx.constraintlayout:constraintlayout-solver:2.0.0-alpha2'
implementation 'androidx.cardview:cardview:1.0.0'
implementation "com.google.firebase:firebase-messaging:17.3.4"
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
implementation "androidx.lifecycle:lifecycle-runtime:2.0.0"

annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.0.0" // use kapt for Kotlin

implementation "de.hdodenhof:circleimageview:2.2.0"
implementation 'androidx.core:core:1.1.0-alpha01'
implementation "com.thoughtbot:expandablerecyclerview:1.0"
implementation "androidx.lifecycle:lifecycle-livedata:2.0.0"
implementation "androidx.lifecycle:lifecycle-viewmodel:2.0.0"
implementation "com.github.franmontiel:FullScreenDialog:1.0.1"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "com.github.apl-devs:appintro:v4.2.3"
implementation "com.google.firebase:firebase-crash:16.2.1"
implementation "com.google.firebase:firebase-core:16.0.5"

【问题讨论】:

  • 您是否启用了 Jetifier?您的某些依赖项可能仍在使用支持库,这可能会导致冲突。
  • @TheWanderer 是的,我做到了
  • 尝试实现androidx.fragment:fragment:1.0.0
  • 看起来ComponentActivityandroidx.activity.ComponentActivityandroidx.core.app.ComponentActivity 处都有。前者实现了 LifecycleOwner 接口,而第二个没有。我认为您的 IDE 正在使用第一个,而运行时正在尝试使用第二个。我想这就是为什么 AndroidX 仍然主要处于 alpha 阶段的原因。
  • @TheWanderer 谢谢就是这样!我删除了 androidx.core 依赖项,它起作用了,将更多地研究 androidx 包并测试我在其他地方不需要它

标签: android android-lifecycle androidx


【解决方案1】:

您应该将androidx.appcompat:appcompat 版本更改为1.1.0-alpha04

【讨论】:

    【解决方案2】:

    刚刚在androidx.appcompat:appcompat 的版本1.1.0-alpha01 中尝试过它现在可以工作了。

    LifecycleOwner 接口现在由ComponentActivity 实现,它由FragmentActivity 扩展 -> AppCompatActivity。您现在应该能够从您的活动中获取lifecycle 对象。

    【讨论】:

      【解决方案3】:

      恢复到 1.0.1 版对我有用。

      implementation "androidx.core:core:1.0.1"
      

      【讨论】:

      • alpha 版本的 1.1.0 有问题
      • find your version ,点击一个可以找到alpha版本
      【解决方案4】:

      在我的情况下,我的问题是 androidx 活动的 1.0.0-alpha05 版本,我恢复到 1.0.0-alpha03 并且它工作正常

      【讨论】:

        猜你喜欢
        • 2019-12-10
        • 1970-01-01
        • 2019-06-27
        • 2019-03-23
        • 1970-01-01
        • 2019-07-08
        • 2019-11-08
        • 1970-01-01
        相关资源
        最近更新 更多