3

I have an app that successfully uses ToggleButton. I am converting the app for use on JELLY BEAN (4.1.1). 4.1.1 has Switch widget which is a better-looking ToggleButton widget. Both widgets derive from CompoundButton.

Android's comparison documentation is here:

http://developer.android.com/guide/topics/ui/controls/togglebutton.html

It says:

The ToggleButton and Switch controls are subclasses of CompoundButton and function in the same manner, so you can implement their behavior the same way.

So what I've done is taken my activity layout file containing ToggleButtons, copied it to the directory res/layout-v14/ and replaced all instances of ToggleButton with Switch. This means Android versions 14 and above will use the layout file with Switch, below 14 will use the layout file with ToggleButton. The XML is identical in each, other than the widget name.

<Switch
 android:id="@+id/settings_some_option_on_off"
 android:textOn="@string/settings_toggle_on"
 android:textOff="@string/settings_toggle_off"
 android:gravity="center"
 android:paddingRight="@dimen/size_padding_minor"
 android:layout_weight="1"
 android:layout_gravity="center"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"/>

In my .java code I'm using CompoundButton only. Not using ToggleButton or Switch at all.

private CompoundButton mViewSomeOptionOnOff;
...
mViewSomeOptionOnOff = (CompoundButton) findViewById(R.id.settings_some_option_on_off);
etc.

When I run on < 14, it works great. Same as before. I get the ToggeButton. When I run on 14, I get a null crash in the Android widget framework.

I downloaded the Android source. From the crash backtrace, I know exactly where the crash is. Switch.java:

808     @Override
809     public void jumpDrawablesToCurrentState() {
810         super.jumpDrawablesToCurrentState();
811         mThumbDrawable.jumpToCurrentState();    <------ boom
812         mTrackDrawable.jumpToCurrentState();
813     }

thumb is a new property to Switch. Even so, I shouldn't have to define it, right? It's not listed as a mandatory property.

Just as a test, back in my -v14 layout, I set android:thumb to a drawable. Then I hit null on line 812, the track. I set android:track to a drawable, and the crash is gone. So what's going on?

Why am I hitting the null crash?

Do I need to find the default Track and Thumb drawables, and copy them in to my app?

Is what I'm trying to do -- using ToggleButton and Switch -- not possible?

Syrinx
  • 81
  • 1
  • 6
  • Look at your logcat backtrace error. It must have a reference to the part of your code that calls Switch.java and its line 811. I think it's when you call your `findViewById(R.id.settings_some_option_on_off)`. Is that correct? – Kyriog Oct 06 '12 at 07:49
  • The backtrace is all Android stuff. No frames inside my app. The crash occurs after `onCreate()`, `onStart()`, and `onResume()` have all successfully completed. – Syrinx Oct 06 '12 at 08:22
  • I should add, the crash occurs just before the activity becomes visible/interactable. – Syrinx Oct 06 '12 at 08:32
  • Hmmm… And what happens if you remove (or comment) all java stuff about this button, in order to let the xml (and only the xml) displaying the switch? If it works, add your java code line per line to identify which part of your code isn't workinq. – Kyriog Oct 06 '12 at 08:33
  • In your AndroidManifest.xml, what's your minSdkVersion and your targetSdkVersion? – Kyriog Oct 06 '12 at 08:51
  • At the moment it is 8/8. I also set it to 14/14 and had the same issue. – Syrinx Oct 06 '12 at 08:55
  • 2
    Maybe there is some problem with the theme of your activity. Maybe the theme that you are using does not have the switch style. I think you need to specify the theme as Holo for apps running on v14 and above. Something like [this](http://stackoverflow.com/questions/10094478/how-to-maintain-backwards-compatibility-while-utilising-android-api-level-15) – nandeesh Oct 06 '12 at 09:05
  • And don't forget to specify your theme in your AndroidManifest.xml by adding `android:theme="@style/AppTheme"` in your `` tag. (thanks nandeesh for hint ;)) – Kyriog Oct 06 '12 at 09:13
  • @nandeesh -- yes that was the solution! I had the theme set to NoTitleBar or something like that. I followed the suggestion at the link you suggested and my app no longer crashes. I'm new to stackoverflow, how do I give you rep points? If you want to post a solution I will give you points if I can. – Syrinx Oct 06 '12 at 18:48
  • @nandeesh's suggestion fixed this for me as well. Please add it as an answer so I can upvote it. :) – cwc Jul 16 '13 at 16:09

2 Answers2

4

make sure your minSdkVersion is 14 on AndroidManifest.xml

0

i had this error as well and solved it by setting theme for api 14 and above - as nandeesh commented.

noam
  • 86
  • 3