Tuesday, July 14, 2015

Android's mysterious XML

Recently I was coding a part of a project which involved launching a new Activity, and creating a new instance of a fragment in said activity. Typical stuff.

So typical, it worked on all versions of Android, but only up until Lollipop.

Why Lollipop, why?

The application would consistantly crash with an XML Inflation error about an unknown class name: <unknown>. Obviously something is up.

Preloading the class in advance ended up fixing the error, so I will leave my resolution here for future reference.

Say for example, the class name in question is com.pyamsoft.fragmentx. If normally launching this fragment in Android Lollipop in a situation similar to the above mentioned fails with an XML inflation error but works on all lower versions of Android (as it should), the following code may assist:

    try {
        Class.forName("com.pyamsoft.fragmentX");
    catch (Throwable e) {
        // You may safely ignore this part if you wish
    }

Place this block before you call the fragment onCreateView to inflate the XML, and you should be able to load up fragments properly. This fix is similar to the work around for the AsyncTask class, which had a loading error in one of the recent Android SDK versions.

For notes about the AsyncTask bug, see bug 81083 in the Android Bug Tracker.

========================
Follow pyamsoft around the Web for updates and announcements about the newest applications!
Like what I do?

Send me an email at: pyam.soft@gmail.com
Or find me online at: https://pyamsoft-official.blogspot.com/

Check out my Android Applications
Check out my Github Repositories
Check out my AUR packages

Follow my FaceBook Page
Follow my Google+ Page
=========================