class PopupLayer extends DialogFragment {
private contentView : ViewGroup | null = null
constructor() {
super()
}
setContentView(view : ViewGroup) {
this.contentView = view
}
override onCreateDialog(savedInstanceState : Bundle | null) : Dialog {
const dialog = super.onCreateDialog(savedInstanceState)
dialog.window?.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT))
dialog.setCanceledOnTouchOutside(false)
dialog.setOnKeyListener((dialog : DialogInterface, keyCode : number, event : KeyEvent) : boolean => {
if (keyCode === KeyEvent.KEYCODE_BACK) {
this.dismiss()
return true
}
return false
})
return dialog
}
override onCreateView(inflater : LayoutInflater, container : ViewGroup | null, savedInstanceState : Bundle | null) : View {
const contentView = this.contentView
if (contentView !== null) {
return contentView
}
// 如果没有设置contentView,返回一个空的FrameLayout
return new FrameLayout(this.requireContext())
}
override onStart() {
super.onStart()
this.dialog?.window?.setLayout(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT
)
}
}
然后我把uvue当做参数传入报错:
java.lang.IllegalStateException: DialogFragment can not be attached to a container view
at androidx.fragment.app.DialogFragment$4.onChanged(DialogFragment.java:150)
at androidx.fragment.app.DialogFragment$4.onChanged(DialogFragment.java:143)
at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133)
at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151)
at androidx.lifecycle.LiveData.setValue(LiveData.java:309)
at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2974)
at androidx.fragment.app.DialogFragment.performCreateView(DialogFragment.java:489)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:224)
at android.os.Looper.loop(Looper.java:318)
at android.app.ActivityThread.main(ActivityThread.java:8747)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:561)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
1***@qq.com (作者)
不好意思,忘记贴调用的代码:
showPopup(component : ViewGroup) : void {
const activity = UTSAndroid.getUniActivity() as FragmentActivity
if (activity == null) {
console.error("Unable to get UniActivity")
return
}
调用:
const show = () => {
const viewElement = uni.getElementById('appp')
const viewGroup = viewElement?.getAndroidView<ViewGroup>()!
console.log('viewGroup', viewGroup)
showPopup(viewGroup)
}
我直接把<template>里面的传进去就报错,和你给我发的案例不一样
2024-09-22 09:46