Home > Software engineering >  androidx.preference.SwitchPreferenceCompat cannot be cast to androidx.preference.PreferenceGroup
androidx.preference.SwitchPreferenceCompat cannot be cast to androidx.preference.PreferenceGroup

Time:03-17

So I'm trying to make an options menu in my android app but when I click on it in the emulator the app crashes and I get the following error:

androidx.preference.SwitchPreferenceCompat cannot be cast to androidx.preference.PreferenceGroup

I don't really know what I am doing wrong here, I hope somebody knows.

The settings activity:

public class SettingsActivity extends AppCompatActivity {
    public static final String KEY_PREF_EXAMPLE_SWITCH = "example_switch";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportFragmentManager().beginTransaction()
                .replace(android.R.id.content,
                        new SettingsFragment()).commit();
    }
}

The fragment class

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState,
                                    String rootKey) {
        setPreferencesFromResource(R.xml.preferences, rootKey);
    }

}

methods in my MainActivity:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        // Launch SettingsActivity
        if (id == R.id.action_settings) {
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

EDIT Here is also my layout file:

<?xml version="1.0" encoding="utf-8"?>
<SwitchPreferenceCompat xmlns:android="http://schemas.android.com/apk/res/android">

    <SwitchPreference
        android:defaultValue="false"
        android:key="vega_preference"
        android:summaryOff="@string/vega_off_string"
        android:summaryOn="@string/vega_on_string"
        android:title="@string/vega_switch_title" />
    <SwitchPreference
        android:defaultValue="false"
        android:key="vegan_preference"
        android:summaryOff="@string/vegan_off_string"
        android:summaryOn="@string/vegan_on_string"
        android:title="@string/vegan_switch_title" />
</SwitchPreferenceCompat>

CodePudding user response:

androidx.preference.SwitchPreferenceCompat cannot be cast to androidx.preference.PreferenceGroup

The snippet setPreferencesFromResource(R.xml.preferences, rootKey) is what raises that as it expects a PreferenceGroup instead of a preference view. Where SwitchPreferenceCompat is not PreferenceGroup.

That is because you used a SwitchPreferenceCompat as the root element of settings screen. Instead you need to use the PreferenceScreen which extends from PreferenceGroup.

So to fix this you need to make sure that the root element of your preference layout is PreferenceScreen

  • Related