Struggling with getting a PreferenceFragmentCompat to show a ListPreference. I believe I have all the proper things set up. String Arrays
<string-array name="categories">
<item>Popular Movies</item>
<item>Top Rated Movies</item>
</string-array>
<string-array name="categories_value">
<item>popular</item>
<item>top_rated</item>
</string-array>
Preferences
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:title="SABRE Settings"
>
<PreferenceCategory android:title="Map Settings"
>
<ListPreference
android:defaultValue="popular"
android:entries="@array/categories"
android:entryValues="@array/categories_value"
android:summary="Select categories of movies"
android:title="Categories"
android:key="@string/movies_categories_key" />
</PreferenceCategory>
</PreferenceScreen>
Strings
<string name="movies_categories_key">categoriesKey</string>
<string name="default_movies_categories">popular</string>
Activity Styling
<style name="Dark" parent="@android:style/Theme.Holo.Light">
<item name="android:windowBackground">@color/dark_back</item>
<item name="android:colorBackground">@color/dark_back</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
</style>
Dependencies
compile ('com.android.support:appcompat-v7:23.4.0')
compile ('com.android.support:preference-v7:23.4.0')
compile ('com.android.support:preference-v14:23.4.0')
compile ('com.android.support:support-v4:23.4.0')
Fragment code
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceManager;
import android.view.View;
import com.trt.pst.R;
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.preferences);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(getResources().getColor(R.color.background_material_light));
}
}
Fragment deployment
getSupportFragmentManager().beginTransaction()
.replace(R.id.single_pane, new SettingsFragment(), "Settings").addToBackStack("RADIO").commit();
The Preferences dialog comes up OK but on pressing to get the expected ListView I get
W/ResourceType: No package identifier when getting value for resource number 0x00000000
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.trt.pst, PID: 26329
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:2558)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:4364)
at android.content.res.Resources.getLayout(Resources.java:2372)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at android.support.v7.app.AlertController$AlertParams.createListView(AlertController.java:880)
at android.support.v7.app.AlertController$AlertParams.apply(AlertController.java:857)
at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:899)
at android.support.v7.preference.PreferenceDialogFragmentCompat.onCreateDialog(PreferenceDialogFragmentCompat.java:154)
at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:308)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Does anyone know what I have done wrong to make this happen?
I had the same problem with dialog theme before. Basically you need to set the theme for dialogs and alertDialogs like:
<style name="Dark" parent="@android:style/Theme.Holo.Light">
...
<item name="android:dialogTheme">@style/Theme.Dialog</item>
<item name="android:alertDialogTheme">@style/Theme.Dialog</item>
</style>
Hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With