I try to implement a day/night system changeable directly from the settings. I used PreferenceFragmentCompat for this but however, when I press the switch preference to change the mode. I have the following error:
E/Parcel: Class not found when unmarshalling: androidx.fragment.app.FragmentManagerState
java.lang.ClassNotFoundException: androidx.fragment.app.FragmentManagerState
Caused by: java.lang.ClassNotFoundException: androidx.fragment.app.FragmentManagerState
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
I looked everywhere but I don't really understand where the error comes from..
This is my Preference Activity:
public class PreferencesActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
if (savedInstanceState == null)
getSupportFragmentManager().beginTransaction().replace(R.id.preferences_container, new PreferencesFragment()).commit();
}
@Override
protected void onDestroy() {
super.onDestroy();
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.contains(getString(R.string.KEY_PREF_NIGHT_MODE)))
AppCompatDelegate.setDefaultNightMode(sharedPreferences.getBoolean(key, false)? AppCompatDelegate.MODE_NIGHT_YES: AppCompatDelegate.MODE_NIGHT_NO);
}
public static class PreferencesFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
}
And I use this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/noteRecyclerBackground"
tools:context=".PreferencesActivity">
<FrameLayout
android:id="@+id/preferences_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
There is no crash but an error and this is when the activity restart with AppCompatDelegate.setDefaultNightMode(sharedPreferences.getBoolean(key, false)? AppCompatDelegate.MODE_NIGHT_YES: AppCompatDelegate.MODE_NIGHT_NO);
Well, per the documentation , this sounds like expected behavior.
If this method is called after any host components with attached AppCompatDelegates have been 'created', a uiMode configuration change will occur in each. This may result in those components being recreated, depending on their manifest configuration .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.