Preferences doesn't work

我怕爱的太早我们不能终老 提交于 2019-12-11 02:39:36

问题


I have a problem with preference in my app.

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

My general screen is ok, but second screen ( network screen) is black (picture below)

And second problem is with ListPreference. It gives me error:

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)

These are my other files:

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

In PreferenceActivity I has only :

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

Can you anybody help me, please?


回答1:


I fixed second problem with nullPointerException. ListPreference doesn't work with integer-array, so I changed it with 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>


来源:https://stackoverflow.com/questions/16193522/preferences-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!