SilverMonkey
SilverMonkey

Reputation: 1001

How is the SwitchPreference intended to be used? (ClassCastException)

I tried to use 2 SwitchPreference in a PreferenceActivity. At the first start of the PreferenceActivity, everything works normal; the activity gets started without problem, the settings.xml is displayed The problems start if one attends to close the PreferenceActivity or to change the state of the SwitchPreference: everytime the app is closed with a ClassCastException. This also occurs without a set dafaultValue. After that, the app will be closed if one attempts to open the PreferenceActivity. Until now i do not access/influence the stored values programmatically in any way, for now i just want display the settings screen for testing purposes. The activity will be started if an menu item gets clicked in the calling activity via

final Intent i = new Intent(this, SettingsActivity.class);
                startActivity(i);  

I tried different suggested ways stated here such as defining boolean ressources and using them to set the defaultValue of the SwitchPreferences or using the method

PreferenceManager.setDefaultValues(this, R.xml.settings, false);

in the main activity. None of that approaches worked. How is the SwitchPreference intended to be used, if it apparently can not save its one state wihtout causing an exception? Do one need to define specific xml attributes to make the preference work?

ClassCastException

    E/AndroidRuntime: FATAL EXCEPTION: main
                      java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
                          at android.app.SharedPreferencesImpl.getBoolean(SharedPreferencesImpl.java:242)
                          at android.preference.Preference.getPersistedBoolean(Preference.java:1637)
                          at android.preference.Preference.persistBoolean(Preference.java:1608)
                          at android.preference.TwoStatePreference.setChecked(TwoStatePreference.java:79)
                          at android.preference.SwitchPreference$Listener.onCheckedChanged(SwitchPreference.java:54)
                          at android.widget.CompoundButton.setChecked(CompoundButton.java:126)
                          at android.widget.Switch.setChecked(Switch.java:688)
                          at android.widget.CompoundButton.toggle(CompoundButton.java:87)
                          at android.widget.CompoundButton.performClick(CompoundButton.java:99)
                          at android.view.View$PerformClick.run(View.java:16966)
                          at android.os.Handler.handleCallback(Handler.java:615)
                          at android.os.Handler.dispatchMessage(Handler.java:92)
                          at android.os.Looper.loop(Looper.java:137)
                          at android.app.ActivityThread.main(ActivityThread.java:4745)
                          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:786)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                          at dalvik.system.NativeStart.main(Native Method)

PreferenceActivity

public class SettingsActivity extends PreferenceActivity{

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.addPreferencesFromResource(R.xml.settings);
  }
}

settings.xml

 <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
    [...]
    <PreferenceCategory
        android:title="@string/sett_not_header">
        <SwitchPreference
            android:key="@+id/sett_not_on"
            android:title="@string/sett_not_act_header"
            android:summary="@string/sett_not_act_summ"
            android:defaultValue="true"/>
        <SwitchPreference
            android:key="@+id/sett_not_mute"
            android:title="@string/sett_not_mute_header"
            android:summary="@string/sett_not_mute_summ"
            android:defaultValue="true"/>
    [...]
    </PreferenceCategory>

</PreferenceScreen>

Upvotes: 0

Views: 600

Answers (1)

SilverMonkey
SilverMonkey

Reputation: 1001

I was able to track down the cause of the exception: In the field android:key i falsely used the expression @+id/in the assumption the key declaration would work the same way the id declaration works. Removing the expression @+id/allowed me to use the SwitchPreference without errors.

Upvotes: 1

Related Questions