bug_ _ _android.app.Fragment$InstantiationException 解决办法

在实际的开发中,我遇到过两次android.app.Fragment$InstantiationException报错。

        其中一次报错,根据报错提示 “make sure class name exists, is public, and has an empty constructor that is public” ,若Fragement定义有带参构造函数,则一定要定义public的默认的构造函数。即可解决此问题。如果硬要携带参数进去,可以通过Intent结合Bunble的方式携带进去。

        第二次报错如下贴的log信息,在“is public, and has an empty constructor that is public” 都满足的情况下,编译ENG版本运行没有任何问题,但是编译USER版本就会出现CRASH错误。原因在于USER版本编译时会代码混淆。

E/AndroidRuntime( 3253): FATAL EXCEPTION: main

E/AndroidRuntime( 3253): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.settings/com.android.settings.SubSettings}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.android.settings.mcwill.McWiLLSettings: make sure class name exists, is public, and has an empty constructor that is public

E/AndroidRuntime( 3253):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1964)
E/AndroidRuntime( 3253):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1989)
E/AndroidRuntime( 3253):     at android.app.ActivityThread.access$600(ActivityThread.java:126)
E/AndroidRuntime( 3253):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1155)
E/AndroidRuntime( 3253):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 3253):     at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 3253):     at android.app.ActivityThread.main(ActivityThread.java:4482)
E/AndroidRuntime( 3253):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3253):     at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 3253):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
E/AndroidRuntime( 3253):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
E/AndroidRuntime( 3253):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 3253): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.android.settings.mcwill.McWiLLSettings: make sure class name exists, is public, and has an empty constructor that is public
E/AndroidRuntime( 3253):     at android.app.Fragment.instantiate(Fragment.java:581)
E/AndroidRuntime( 3253):     at android.preference.PreferenceActivity.switchToHeaderInner(PreferenceActivity.java:1120)
E/AndroidRuntime( 3253):     at android.preference.PreferenceActivity.switchToHeader(PreferenceActivity.java:1136)
E/AndroidRuntime( 3253):     at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:532)
E/AndroidRuntime( 3253):     at com.android.settings.Settings.onCreate(Settings.java:99)
E/AndroidRuntime( 3253):     at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime( 3253):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime( 3253):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
E/AndroidRuntime( 3253):     ... 11 more
E/AndroidRuntime( 3253): Caused by: java.lang.ClassNotFoundException: com.android.settings.mcwill.McWiLLSettings
E/AndroidRuntime( 3253):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
E/AndroidRuntime( 3253):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime( 3253):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime( 3253):     at android.app.Fragment.instantiate(Fragment.java:571)
E/AndroidRuntime( 3253):     ... 18 more
W/ActivityManager(  422):   Force finishing activity com.android.settings/.SubSettings
W/ActivityManager(  422):   Force finishing activity com.android.settings/.Settings

        解决办法:

        在对应模块的proguard.flags文件中引入该Fragment所在的类包即可。比如: -keep class com.android.settings.wifi.*Settings

转载于:https://www.cnblogs.com/awkflf11/p/4613448.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值