PreferenceFragment 崩溃,空对象引用

PreferenceFragment crashing, Null object Reference

我有一个简单的 ActionBarActivity,它包含一个 PreferenceFragment,如下所示:

import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceFragment;

public class SettingsActivity extends Activity {
    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(R.id.fra_settings_container, new SettingsFragment()).commit();
}
public static class SettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }
}
}

这是我的 preferences.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<ListPreference
    android:defaultValue="15"
    android:dialogTitle="@string/settings_heading_alarm_interval"
    android:entries="@array/alarm_interval_entries"
    android:entryValues="@array/alarm_interval_values"
    android:key="alarm_interval"
    android:title="@string/settings_heading_alarm_interval" />
<ListPreference
    android:defaultValue="1"
    android:dialogTitle="@string/settings_heading_alarm_type"
    android:entries="@array/alarm_type_entries"
    android:entryValues="@array/alarm_type_values"
    android:key="alarm_type"
    android:title="@string/settings_heading_alarm_type"
/>

<CheckBoxPreference
    android:key="alarm_vibrate_only"
    android:title="@string/settings_heading_silent" />

<RingtonePreference
    android:dependency="alarm_vibrate_only"
    android:key="alarm_tone"
    android:title="@string/settings_heading_alarm_tone"
    android:showDefault="true"
    android:showSilent="true"/>

</PreferenceScreen>

当我导航到 SettingsActivity 时,我收到以下错误日志:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
        at android.preference.ListPreference.findIndexOfValue(ListPreference.java:231)
        at android.preference.ListPreference.getValueIndex(ListPreference.java:240)
        at android.preference.ListPreference.getEntry(ListPreference.java:218)
        at android.preference.ListPreference.getSummary(ListPreference.java:164)
        at android.preference.Preference.onBindView(Preference.java:550)
        at android.preference.Preference.getView(Preference.java:489)
        at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:246)
        at android.widget.AbsListView.obtainView(AbsListView.java:2344)
        at android.widget.ListView.makeAndAddView(ListView.java:1864)
        at android.widget.ListView.fillDown(ListView.java:698)
        at android.widget.ListView.fillFromTop(ListView.java:759)
        at android.widget.ListView.layoutChildren(ListView.java:1659)
        at android.widget.AbsListView.onLayout(AbsListView.java:2148)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

根据我的理解,我正在完全按照 Android 指南实施 PreferenceFragment,但我在错误日志中找不到任何告诉我在哪里寻找问题的信息(很可能是因为我还是初学者)。由于我已经看到几乎所有与我正在处理的问题远程相关的问题,因此可能值得注意的是我是 运行 Lollipop,并且我正在使用 Theme.Appcompat.Light。我的应用程序的其余部分运行良好,所以我不明白主题怎么可能是问题。

谢谢,如果我能提供更多信息,请告诉我。

编辑:

以下是从我的 string_arrays.xml 文件中获取的值:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="alarm_type_entries">
    <item name="alarm_continuous">Continuous</item>
    <item name="alarm_single">Sound Once</item>
    <item name="alarm_minute">Sound Each Minute</item>
</string-array>

<string-array name="alarm_type_values">

    <item name="continuous">1</item>
    <item name="single">2</item>
    <item name="minute">3</item>

</string-array>

<string-array name="alarm_interval_entries">

    <item name="interval_fifteen">15 Minutes</item>
    <item name="interval_thirty">30 Minutes</item>
    <item name="interval_forty_five">45 Minutes</item>
    <item name="interval_one_hour">1 Hour</item>
    <item name="interval_two_hours">2 Hours</item>
    <item name="interval_three_hours">3 Hours</item>

</string-array>

<integer-array name="alarm_interval_values">

    <item name="fifteen">15</item>
    <item name="thirty">30</item>
    <item name="forty_five">45</item>
    <item name="one_hour">60</item>
    <item name="two_hours">120</item>
    <item name="three_hours">180</item>

</integer-array>
</resources>

感谢 njzk2 指出这是一个 ListPreference 查找空值的问题。事实证明,其中一个数组存储为 "integer-array" 而不是 "string-array"。更改它解决了问题。

更改来自

的所有项目
<integer-array ...

<string-array ...

为我工作。