启动 PreferenceActivity 时出现 NullPointerException

NullPointerException When Starting PreferenceActivity

SOLUTION
In my arrays.xml file, the entry arrays were <string-array/> and the value arrays were <integer-array/>. My solution was to make the integer arrays string arrays.

我正在尝试为我的应用程序创建设置 activity,但我 运行 遇到了很多问题。启动 activity 时,我一直收到 NullReferenceException。这是我的代码:
CalculatorActivity.onOptionsItemSelected(MenuItem item):

int id = item.getItemId();
if (id == R.id.action_settings) {
    startActivity(new Intent(CalculatorActivity.this, SettingsActivity.class));
    return true;
}

SettingsActivity.onCreate(Bundle savedInstanceState):

setTheme(R.style.SettingsTheme);
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferenceFragment() {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}).commit();

preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory
        android:title="@string/pref_1">
        <ListPreference
            android:entries="@array/tempUnitsEntries"
            android:title="@string/temp_title"
            android:dialogTitle="@string/temp_title"
            android:key="@string/temp_key"
            android:entryValues="@array/tempUnitsValues"
            android:defaultValue="0"/>
        <ListPreference
            android:title="@string/dist_title"
            android:dialogTitle="@string/dist_title"
            android:key="@string/dist_key"
            android:entryValues="@array/distUnitsValues"
            android:entries="@array/distUnitsEntries"
            android:defaultValue="0"/>
        <ListPreference
            android:entryValues="@array/speedUnitsValues"
            android:entries="@array/speedUnitsEntries"
            android:title="@string/speed_title"
            android:dialogTitle="@string/speed_title"
            android:key="@string/speed_key"
            android:defaultValue="0"/>
        <ListPreference
            android:entryValues="@array/psrUnitsValues"
            android:entries="@array/psrUnitsEntries"
            android:title="@string/psr_title"
            android:dialogTitle="@string/psr_title"
            android:key="@string/psr_key"
            android:defaultValue="0"/>
    </PreferenceCategory>
    <PreferenceCategory
        android:title="@string/pref_2">
        <CheckBoxPreference
            android:key="@string/autoGet_key"
            android:defaultValue="true"
            android:title="@string/autoGet_title"/>
    </PreferenceCategory>
    <PreferenceCategory
        android:title="@string/pref_3">
        <SwitchPreference
            android:key="@string/vbMain_key"
            android:defaultValue="false"/>
        <!-- THIS BLOCK WAS CAUSING ANOTHER ERROR -->
        <!-- <MultiSelectListPreference
            android:entryValues="@array/verboseModeDataValues"
            android:dialogTitle="@string/vbData_title"
            android:title="@string/vbData_title"
            android:entries="@array/verboseModeDataEntries"
            android:key="@string/vbData_key"/> -->
    </PreferenceCategory>
</PreferenceScreen>

这是堆栈跟踪 (请注意 none 的条目与我的应用有关):

05-25 10:25:50.342: E/AndroidRuntime(3744): FATAL EXCEPTION: main
05-25 10:25:50.342: E/AndroidRuntime(3744): Process: com.example.myapp PID: 3744
05-25 10:25:50.342: E/AndroidRuntime(3744): java.lang.NullPointerException
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.preference.ListPreference.findIndexOfValue(ListPreference.java:223)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.preference.ListPreference.getValueIndex(ListPreference.java:232)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.preference.ListPreference.getEntry(ListPreference.java:210)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.preference.ListPreference.getSummary(ListPreference.java:156)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.preference.Preference.onBindView(Preference.java:524)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.preference.Preference.getView(Preference.java:462)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.AbsListView.obtainView(AbsListView.java:2263)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.ListView.makeAndAddView(ListView.java:1790)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.ListView.fillDown(ListView.java:691)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.ListView.fillFromTop(ListView.java:752)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.ListView.layoutChildren(ListView.java:1630)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.AbsListView.onLayout(AbsListView.java:2091)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.View.layout(View.java:14817)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.View.layout(View.java:14817)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.View.layout(View.java:14817)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.View.layout(View.java:14817)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.View.layout(View.java:14817)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.Choreographer.doFrame(Choreographer.java:544)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.os.Handler.handleCallback(Handler.java:733)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.os.Looper.loop(Looper.java:136)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at java.lang.reflect.Method.invoke(Method.java:515)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-25 10:25:50.342: E/AndroidRuntime(3744):     at dalvik.system.NativeStart.main(Native Method)

此外,我要注意这些行出现在异常之前的 LogCat 中:

05-25 10:25:42.941: W/EGL_emulation(3744): eglSurfaceAttrib not implemented
05-25 10:25:47.851: W/Resources(3744): Converting to string: TypedValue{t=0x10/d=0x0 a=-1}
05-25 10:25:47.991: W/Resources(3744): Converting to string: TypedValue{t=0x10/d=0x0 a=-1}
05-25 10:25:47.991: W/Resources(3744): Converting to string: TypedValue{t=0x10/d=0x0 a=-1}
05-25 10:25:47.991: W/Resources(3744): Converting to string: TypedValue{t=0x10/d=0x0 a=-1}
05-25 10:25:49.131: W/EGL_emulation(3744): eglSurfaceAttrib not implemented
05-25 10:25:50.252: D/AndroidRuntime(3744): Shutting down VM
05-25 10:25:50.252: W/dalvikvm(3744): threadid=1: thread exiting with uncaught exception (group=0xb0d09b20)

在我的 arrays.xml 文件中,条目数组是 <string-array/>,值数组是 <integer-array/>。我的解决方案是制作整数数组字符串数组。这似乎解决了问题。