Ren*_*ele 13 android android-overlay android-jetpack-compose
当我尝试使用 XML 在覆盖层中插入 Compose(在其他应用程序上绘制)时,出现以下异常:
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.constraintlayout.widget.ConstraintLayout{d596746 V.E...... ......ID 0,0-0,0}
Run Code Online (Sandbox Code Playgroud)
但如果没有覆盖(在活动中),它可以正常工作。有谁知道如何解决吗?我已经将 AppCompat 库更新到 1.3.0
我的 XML 代码:
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.constraintlayout.widget.ConstraintLayout{d596746 V.E...... ......ID 0,0-0,0}
Run Code Online (Sandbox Code Playgroud)
我的叠加代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">
<androidx.compose.ui.platform.ComposeView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/compose_view"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
对我来说,这是因为我没有包含appcompat库,并且我的活动继承自Activity而不是AppCompatActivity。通过添加库解决了问题:
implementation("androidx.appcompat:appcompat:1.3.1")
Run Code Online (Sandbox Code Playgroud)
并继承自AppCompatActivity:
class MyActivity: AppCompatActivity() {
...
}
Run Code Online (Sandbox Code Playgroud)
小智 6
对我来说,androidx.appcompat:appcompat
从升级1.0.0
到1.4.1
,问题就解决了。
片段:
class Xxx : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_xxx, container, false)
view.findViewById<ComposeView>(R.id.compose_view).apply {
setViewCompositionStrategy(
ViewCompositionStrategy.DisposeOnLifecycleDestroyed(viewLifecycleOwner)
)
setContent {
// compose
}
}
return view
}
Run Code Online (Sandbox Code Playgroud)
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<androidx.compose.ui.platform.ComposeView
android:id="@+id/compose_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
build.gradle
文件中的最新版本。dependencies { ... \n implementation \'androidx.constraintlayout:constraintlayout:1.3.x\'\n
Run Code Online (Sandbox Code Playgroud)\n<androidx.constraintlayout.ConstraintLayout>\n\n //with ->\n\n<androidx.constraintlayout.widget.ConstraintLayout>\n\n \n
Run Code Online (Sandbox Code Playgroud)\n在每个地方CTRL+ SHIFT+ R\n_
\ngradle.properties
添加这些: android.enableJetifier=true\n android.useAndroidX=true\n
Run Code Online (Sandbox Code Playgroud)\n\n\n文件\xe2\x86\x92使缓存无效/重新启动\xe2\x86\x92使缓存无效并重新启动
\n
归档时间: |
|
查看次数: |
9018 次 |
最近记录: |