偏好不起作用

时间:2013-04-24 13:39:37

标签: android preferences

我的应用中存在偏好问题。

我创建了preference.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">


        <!-- General screen  -->
        <PreferenceCategory
                android:title="@string/general_category"
                android:key="prefGeneralCategory">
        <CheckBoxPreference
                        android:icon="@drawable/content_discard"
                        android:title="@string/pref_general_vibration"
                        android:defaultValue="false"
                        android:key="prefVibration"
                        android:summaryOn="checked"
                        android:summaryOff="unchecked" 
                        />
                       <!--  android:summary="@string/pref_general_vibration_description" -->

        <CheckBoxPreference
                        android:title="@string/pref_general_record_battery"
                        android:defaultValue="true"
                        android:summary="@string/pref_general_record_battery_description"
                        android:key="prefRecordBattery" />

        <ListPreference
                        android:title="@string/pref_network_type"
                        android:summary="@string/pref_network_type_description"
                        android:key="prefNetworkType"
                        android:defaultValue="1"
                        android:entries="@array/prefNetworkArray"
                        android:entryValues="@array/prefNetworkValues" />
        </PreferenceCategory>

        <!-- Network screen  -->

        <PreferenceCategory
                android:title="@string/pref_network_screen"
                android:key="prefNetworkCategory">

        <PreferenceScreen
                android:title="@string/pref_network_screen"
                android:key="prefNetworkScreen"
                >
                <CheckBoxPreference
                        android:title="@string/pref_network_update"
                        android:defaultValue="false"
                        android:summary="@string/pref_network_update_description"
                        android:key="prefUpdate" />

         </PreferenceScreen>
         </PreferenceCategory>
</PreferenceScreen>

我的常规屏幕没问题,但第二个屏幕(网络屏幕)是黑色的(下图)

enter image description here

第二个问题是ListPreference。它给了我错误:

04-24 15:20:29.689: E/AndroidRuntime(399): FATAL EXCEPTION: main
04-24 15:20:29.689: E/AndroidRuntime(399): java.lang.NullPointerException
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.preference.ListPreference.findIndexOfValue(ListPreference.java:169)
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.preference.ListPreference.getValueIndex(ListPreference.java:178)
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.preference.ListPreference.onPrepareDialogBuilder(ListPreference.java:190)
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.preference.DialogPreference.showDialog(DialogPreference.java:293)
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.preference.DialogPreference.onClick(DialogPreference.java:264)
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.preference.Preference.performClick(Preference.java:810)
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:190)
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.widget.ListView.performItemClick(ListView.java:3513)
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.os.Handler.handleCallback(Handler.java:587)
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.os.Looper.loop(Looper.java:123)
04-24 15:20:29.689: E/AndroidRuntime(399):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-24 15:20:29.689: E/AndroidRuntime(399):  at java.lang.reflect.Method.invokeNative(Native Method)
04-24 15:20:29.689: E/AndroidRuntime(399):  at java.lang.reflect.Method.invoke(Method.java:507)
04-24 15:20:29.689: E/AndroidRuntime(399):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-24 15:20:29.689: E/AndroidRuntime(399):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-24 15:20:29.689: E/AndroidRuntime(399):  at dalvik.system.NativeStart.main(Native Method)

这些是我的其他文件:

<resources>
...

    <string name="general_category">Obecné</string>
        <string name="pref_general_vibration">Povolit vibrace</string>
        <string name="pref_general_record_battery">Kontrola baterie</string>
        <string name="pref_general_record_battery_description">vypnout nahrávání, pokud je baterie pod 20%</string>


    <string name="pref_network_screen">Síťové nastavení</string>
        <string name="pref_network_type">Volba sítě</string>
        <string name="pref_network_type_description">Vyberte typ připojení</string>
        <string name="pref_network_update">Automatické aktualizace</string>
        <string name="pref_network_update_description">Povolení k automatické aktualizace aplikace</string>

    <string-array name="prefNetworkArray">
    <item name="net1">wifi (default)</item>
    <item name="net2">3G</item>
    <item name="net3">wifi/3G</item>
    </string-array>
    <integer-array name="prefNetworkValues">
    <item name="net1">1</item>
    <item name="net2">2</item>
    <item name="net3">3</item>
    </integer-array>

...

</resources>

在PreferenceActivity中我只有:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.preferences);   } 

请问有人帮助我吗?

1 个答案:

答案 0 :(得分:2)

我用nullPointerException修复了第二个问题。 ListPreference不适用于整数数组,所以我用string-array更改了它。

<string-array name="prefNetworkArray">
<item name="net1">wifi (default)</item>
<item name="net2">3G</item>
<item name="net3">wifi/3G</item>
</string-array>
<string-array name="prefNetworkValues">
<item name="net1">1</item>
<item name="net2">2</item>
<item name="net3">3</item>
</string-array>