简体   繁体   中英

How to set Preferences?

Is there any Wrong in this code to set Preference.. Because I am getting error "Unfortunately (App name) has stopped. Please help me. Is there any alternative method instead of addPreferenceFromResource(R.xml.preference);

package example.katta;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;

public class Preference extends PreferenceActivity{

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content,new MyPreferenceFragment()).commit();
}

public static class MyPreferenceFragment extends PreferenceFragment
{
    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference);
    }
}


}

Here is My xml file:

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

<EditTextPreference
    android:key="Name"
    android:summary="Enter Your Name"
    android:title="EditText" />

<CheckBoxPreference
    android:defaultValue="true"
    android:key="checkbox"
    android:summary="Check This Box"
    android:title="Checkbox" />

<ListPreference
    android:entries="@array/list"
    android:entryValues="@+array/listvalue"
    android:key="list"
    android:summary="This is The List to choose From"
    android:title="List" />

</PreferenceScreen>

Here is My array String.

<string-array name="list">
    <item>Option 1</item>
    <item>Option 2</item>
    <item>Option 3</item>
    <item>Option 4</item>
</string-array>
<string-array name="value">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
</string-array>

</resources>

This line is not valid:

android:entryValues="@+array/listvalue"

You should replace it with:

android:entryValues="@array/listvalue"

EDIT:
the array resource has another name:

android:entryValues="@array/value"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM