問題
按照官網(wǎng)步驟集成Compose胯盯,使用Android Studio創(chuàng)建Compose項(xiàng)目編譯成功暗赶,但是在真機(jī)運(yùn)行會(huì)閃退呛梆。
1.下載 Android Studio Arctic Fox
2.按照步驟創(chuàng)建新Compose項(xiàng)目
3.然而運(yùn)行會(huì)報(bào)錯(cuò),報(bào)錯(cuò)如下
2021-08-03 10:56:10.728 2496-2496/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hongjiefly.jetpackcompose, PID: 2496
java.lang.AbstractMethodError: abstract method "void androidx.lifecycle.DefaultLifecycleObserver.onCreate(androidx.lifecycle.LifecycleOwner)"
at androidx.lifecycle.FullLifecycleObserverAdapter.onStateChanged(FullLifecycleObserverAdapter.java:36)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:196)
at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.android.kt:808)
at android.view.View.dispatchAttachedToWindow(View.java:20626)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3514)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3521)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3521)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3521)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3521)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2672)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2152)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8675)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1352)
at android.view.Choreographer.doCallbacks(Choreographer.java:1149)
at android.view.Choreographer.doFrame(Choreographer.java:1049)
at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:1275)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:233)
at android.app.ActivityThread.main(ActivityThread.java:8010)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
解決方案
原代碼 minSdk 21
minSdk 21 改成 minSdk 24
即可正常運(yùn)行咆槽,看來文檔寫的不靠譜。我的真機(jī)是Android10的冻记。