Android PreferenceFragmentCompat在ListPreference视图上崩溃

时间:2017-05-04 13:55:31

标签: android android-fragments android-preferences

在获取PreferenceFragmentCompat以显示ListPreference时苦苦挣扎。 我相信我已经设置了所有适当的东西。字符串数组

<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>

选择

<?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>

的字符串

<string name="movies_categories_key">categoriesKey</string>
<string name="default_movies_categories">popular</string>

活动样式

    <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>

依赖

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')

片段代码

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));
}

}

片段部署

getSupportFragmentManager().beginTransaction()
                    .replace(R.id.single_pane, new SettingsFragment(), "Settings").addToBackStack("RADIO").commit();

“首选项”对话框出现正常但按下以获取预期的ListView我得到

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)

有谁知道我做错了才能实现这个目标?

1 个答案:

答案 0 :(得分:0)

之前我遇到过与对话框主题相同的问题。基本上你需要设置对话框和alertDialogs的主题,如:

<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>

希望有所帮助