Question

I'm trying to add a fragment into the Android Settings app to customize the ROM I'm working on. This is the error I'm getting when it force closes according to the logcat.

E/AndroidRuntime(31496): FATAL EXCEPTION: main
E/AndroidRuntime(31496): 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.pcf.RomSettings: make sure class name exists, is public, and has an empty constructor that is public
E/AndroidRuntime(31496):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
E/AndroidRuntime(31496):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
E/AndroidRuntime(31496):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
E/AndroidRuntime(31496):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
E/AndroidRuntime(31496):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(31496):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(31496):    at android.app.ActivityThread.main(ActivityThread.java:5193)
E/AndroidRuntime(31496):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(31496):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(31496):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
E/AndroidRuntime(31496):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
E/AndroidRuntime(31496):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(31496): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.android.settings.pcf.RomSettings: make sure class name exists, is public, and has an empty constructor that is public
E/AndroidRuntime(31496):    at android.app.Fragment.instantiate(Fragment.java:592)
E/AndroidRuntime(31496):    at android.preference.PreferenceActivity.switchToHeaderInner(PreferenceActivity.java:1138)
E/AndroidRuntime(31496):    at android.preference.PreferenceActivity.switchToHeader(PreferenceActivity.java:1154)
E/AndroidRuntime(31496):    at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:539)
E/AndroidRuntime(31496):    at com.android.settings.Settings.onCreate(Settings.java:152)
E/AndroidRuntime(31496):    at android.app.Activity.performCreate(Activity.java:5104)
E/AndroidRuntime(31496):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
E/AndroidRuntime(31496):    at E/AndroidRuntime(31496):     ... 11 more
E/AndroidRuntime(31496): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.android.settings.pcf.RomSettings" on path: /system/app/Settings.apk
E/AndroidRuntime(31496):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
E/AndroidRuntime(31496):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime(31496):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime(31496):    at android.app.Fragment.instantiate(Fragment.java:582)
E/AndroidRuntime(31496):    ... 18 more

The class is public and everything. I'm sure the name is right. Here is a link to my Github with the file: https://github.com/konstantinkeller/android_packages_apps_Settings/blob/jb4.2.1/src/com/android/settings/pcf/RomSettings.java

That's the entire package. You can check the XML and everything also. Can anyone help me? Thanks.

Was it helpful?

Solution

I guess the error message is right and your class just isn't in the apk. Try to add your file/package to the proguard.flags file.

Proguard probably removed the file if it isn't referenced in your code but only as text in xml files.

ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes.

OTHER TIPS

Adding to kapep's answer, you should know some bleeding-edge Android features sometimes provide bad java reference generation from XML-file references, like IDs in R.java and other sources in gen/.

I had this happen to me when using xml/headers.xml for interfacing my PreferenceActivity and custom PreferenceFragments, with "android:fragment" referencing the later ones (the ONLY reference in the ENTIRE project). Proguard just excluded them since no gen/*.java reference was being created properly. Android build tools basically uses the files kept after an Eclipse build, and these DO NOT SEEM TO INCLUDE "android:fragment" references, so the custom classes are detected never to be used by the OS at runtime and proguard just deletes them. I believe an ADT bug might be the culprit on this bad reference-generation.

Long story short - use "-keep packagename.** { *; }" on proguard configuration whenever you want to debug this problem's root cause. If it gets solved, you can either try updating ADT, cleaning the project to re-trigger gen/ creation, and packaging without -keep. If still fails, just leave the -keep directive and never think about it again.

You need to use a different path for your classes. com.android.settings is part of the android system. Use a domain you own.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top