I'm currently using the following Switch open-source compatibility library:
https://github.com/ankri/SwitchCompatLibrary
So far it works well, except on some devices (last reported on Android 4.0.4), the app crashes the first time the activity (containing a Switch) is opened! Then on following run everything works fine.
I've tried to apply the style (that includes the Switch style) in various ways, but nothing seems to solve it:
It's as if on first run the theme is not applied and the app crashes. Then on app restart the same Activity will be ok!?
There is another compatibility library I tried, from here: https://github.com/BoD/android-switch-backport
But I get the exact same results!
Here is the stack trace when app crashes:
------------
java.lang.NullPointerException
at de.ankri.views.Switch.onMeasure(SourceFile:514)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4704)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4704)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12723)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4704)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
at android.view.View.measure(View.java:12723)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1068)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2455)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4462)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:573)
at dalvik.system.NativeStart.main(Native Method)
------------
The line code where it happens:
mTrackDrawable = a.getDrawable(R.styleable.Switch_track);
mTrackDrawable.getPadding(mTempRect);
It no longer crashes because of the text style missing because I set them up manually on app start.
Here is the track styles in various files of the compatibility project, from the theme being used:
v11:
<style name="AppThemeDark" parent="@android:style/Theme">
<item name="switchStyle">@style/switch_dark</item>
<item name="textAppearance">@style/TextAppearance</item>
</style>
v14:
<style name="AppThemeDark" parent="@android:style/Theme.DeviceDefault">
<item name="switchStyle">@style/switch_dark</item>
<item name="textAppearance">@style/TextAppearance</item>
</style>
common:
<style name="switch_dark">
<item name="track">@drawable/switch_track_holo_dark</item>
<item name="thumb">@drawable/switch_inner_holo_dark</item>
<item name="textOn">@string/textOn</item>
<item name="textOff">@string/textOff</item>
<item name="thumbTextPadding">12dip</item>
<item name="switchMinWidth">96dip</item>
<item name="switchPadding">16dip</item>
<item name="switchTextAppearance">@style/TextAppearance</item>
</style>
Anyone used one of those libraries and faced such issue?
I finally solved this, with a number of "tricks" I found in various posts and web sites.
Originally the theme was defined in the Manifest.xml on the Application tag only.
1- Applied the same theme to every activity defined in the Manifest.xml 2- Customized any transparent activity to use a custom theme that would include the switch theme to make sure the app would never start without it 3- Added a setTheme in onCreate() of the java Application class to make sure the appropriate theme is set when application starts.
Not sure if all 3 are needed, but it worked. Thanks to a user who could reproduce the issue consistently: App started for the first time: FC in activity using a Switch, then restart no FC!?