我有一个简单的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查找空值是一个问题。事实证明,其中一个数组存储为“整数数组”而不是“字符串数组”。改变那解决了问题。

关于android - PreferenceFragment崩溃,空对象引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28093883/

10-11 00:27