简体   繁体   English

动态功能:无法实例化片段:确保 class 名称存在

[英]Dynamic feature: Unable to instantiate fragment : make sure class name exists

I'm trying to load my fragment that lives in a dynamic feature module.我正在尝试加载位于动态功能模块中的片段。 However, I keep getting a crash:但是,我一直在崩溃:

val f = FragmentFactory().instantiate(classLoader,
 "com.myapp.customviewdynamicfeature.CustomFragment")

supportFragmentManager.beginTransaction()
.add(R.id.customFragmentContainer, f).commit()
2022-03-25 11:36:54.020 20732-20732/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myapp.dynamicdeliverysampleapp, PID: 20732
    androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.myapp.customviewdynamicfeature.CustomFragment: make sure class name exists
        at androidx.fragment.app.FragmentFactory.loadFragmentClass(FragmentFactory.java:97)
        at 

I also tried adding this to my dynamic module's proguard我还尝试将其添加到我的动态模块的 proguard

-keep class com.myapp.customviewdynamicfeature.** { *; }

I've also tried reflection:我也尝试过反思:

val f = Class.forName("com.myapp.customviewdynamicfeature.CustomFragment").newInstance() as Fragment

but I get a similar error:但我得到一个类似的错误:

    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:558)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
     Caused by: java.lang.ClassNotFoundException: com.myapp.customviewdynamicfeature.CustomFragment

I believe I resolved this by upgrading the AndroidX fragment version to 1.5.0.我相信我通过将 AndroidX 片段版本升级到 1.5.0 解决了这个问题。 https://developer.android.com/jetpack/androidx/releases/fragment#version_15_2 https://developer.android.com/jetpack/androidx/releases/fragment#version_15_2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法实例化片段确保类名存在 - Unable to instantiate fragment make sure class name exists 无法实例化片段确保类名存在,是公共的,并且具有公共的空构造函数 - Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public 强制关闭:无法实例化片段,请确保类名称存在,为公共名称并且具有一个空的公共构造函数 - FORCE CLOSE: Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public InstantiationException:无法实例化片段确保类名存在,是公共的,并且具有公共的空构造函数 - InstantiationException: Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public KOTLIN-> InstantiationException:无法实例化片段,请确保类名存在,是公共的,并且具有一个空的,公共的构造函数 - KOTLIN -> InstantiationException: Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public 无法实例化片段com.example.Fragments.r:确保类名存在,是公共的,并且具有一个空的公共构造函数 - Unable to instantiate fragment com.example.Fragments.r: make sure class name exists, is public, and has an empty constructor that is public 确保类名存在,是公共的,并且具有带有类名和空构造函数的公共片段的空构造函数 - make sure class name exists, is public, and has an empty constructor for public fragment with class name and empty constructor 类扩展Fragment-错误说确保类名存在,是公共的并且有一个空的,公共的构造函数 - Class extending Fragment - error says make sure class name exists, is public, and has an empty constructor that is public 我在Fragment中添加了空的构造函数。 但仍然“确保类名存在,是公共的,并且有一个空的公共构造函数” - I have added empty constructor in Fragment. but still “make sure class name exists, is public, and has an empty constructor that is public” 通过类名动态实例化Fragment - Instantiate Fragment dynamically by class name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM