1***@qq.com
1***@qq.com
  • 发布:2024-09-20 18:30
  • 更新:2024-09-20 18:45
  • 阅读:111

使用uts插件编写的原生弹窗view,没法插入uvue组件

分类:uni-app x
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)

2024-09-20 18:30 负责人:无 分享
已邀请:
DCloud_Android_DQQ

DCloud_Android_DQQ

原生api : DialogFragment 的用法问题,参考一下 https://blog.csdn.net/yztbydh/article/details/86502133

  • 1***@qq.com (作者)

    不好意思,忘记贴调用的代码:

    showPopup(component : ViewGroup) : void {

    const activity = UTSAndroid.getUniActivity() as FragmentActivity

    if (activity == null) {

    console.error("Unable to get UniActivity")

    return

    }


        UTSAndroid.getDispatcher("main").async(function (_) {  
    try {
    const popupLayer = new PopupLayer()
    popupLayer.setContentView(component)
    popupLayer.show(activity.supportFragmentManager, "PopupLayer")
    this.popupInstance = popupLayer
    } catch (error) {
    console.error("Error showing popup:", error)
    }
    })
    }

    调用:

    const show = () => {

    const viewElement = uni.getElementById('appp')

    const viewGroup = viewElement?.getAndroidView<ViewGroup>()!

    console.log('viewGroup', viewGroup)

    showPopup(viewGroup)


    isPopupVisible.value = true  

    }


    我直接把<template>里面的传进去就报错,和你给我发的案例不一样

    2024-09-22 09:46

要回复问题请先登录注册