当我尝试用XML插入Compose (在其他应用程序上)时,我得到了以下例外:
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.constraintlayout.widget.ConstraintLayout{d596746 V.E...... ......ID 0,0-0,0}
但没有覆盖(在活动中),它的工作正常。有人知道怎么解决吗?我已经将AppCompat库更新为1.3.0
我的XML代码:
<?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>
我的覆盖代码:
mParams = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
)
layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
mView = layoutInflater.inflate(R.layout.power_overlay, null)
mParams!!.gravity = Gravity.CENTER
mWindowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
mWindowManager.addView(mView, mParams)
发布于 2021-09-17 09:11:39
对我来说,这是因为我没有包含appcompat库,我的活动继承自activity,而不是AppCompatActivity。通过添加库解决了问题:
implementation("androidx.appcompat:appcompat:1.3.1")
并继承自AppCompatActivity
class MyActivity: AppCompatActivity() {
...
}
发布于 2022-05-11 10:06:27
对我来说,将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
}
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>
发布于 2022-08-31 19:18:05
我写了这段代码,它起作用了。
class AndroidComposeDialog<T>(
private val activity: T,
private val content: @Composable () -> Unit
) : Dialog(activity) where T : Context, T : androidx.lifecycle.LifecycleOwner, T : ViewModelStoreOwner, T : SavedStateRegistryOwner, T : OnBackPressedDispatcherOwner {
private fun initViewTreeOwners() {
val window = window ?: return
ViewTreeLifecycleOwner.set(window.decorView, activity)
ViewTreeViewModelStoreOwner.set(window.decorView, activity)
window.decorView.setViewTreeSavedStateRegistryOwner(activity)
window.decorView.setViewTreeOnBackPressedDispatcherOwner(activity)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//here i init view tree owner that will be used by compose view
initViewTreeOwners()
//here i hide background of dialog
window?.setBackgroundDrawable(ColorDrawable(android.graphics.Color.TRANSPARENT))
val view = ComposeView(context).apply {
setContent(content)
}
setContentView(view)
}
}
// A function that returns an android dialog based on entered content
@Composable
fun androidDialog(content: @Composable () -> Unit): AndroidComposeDialog<*>? {
val context = LocalContext.current
val activity = context as? AppCompatActivity
val componentActivity = context as? ComponentActivity
val dialog = remember(activity, content.hashCode()) {
if (activity != null)
AndroidComposeDialog(activity = activity, content = content)
else if (componentActivity != null)
AndroidComposeDialog(activity = componentActivity, content = content)
else null
}
return dialog
}
现在它已经准备好了
@Composable
fun ShowDialog() {
val dialog = androidDialog {
Box(
modifier = Modifier
.size(100.dp)
.clip(RoundedCornerShape(25.dp))
.background(Color.Blue)
)
}
Box(modifier = Modifier.fillMaxSize()) {
Button(onClick = {
dialog?.show()
}, modifier = Modifier.align(Alignment.Center)) {
Text(text = "show dialog")
}
}
}
https://stackoverflow.com/questions/68395975
复制相似问题