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