如何设置首选项?

时间:2015-08-09 10:58:49

标签: java android

此代码中是否有任何错误设置首选项。因为我收到错误"不幸(应用程序名称)已停止。请帮我。是否有替代方法而不是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);
    }
}


}

这是我的xml文件:

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

这是我的数组字符串。
    

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

1 个答案:

答案 0 :(得分:0)

此行无效:

android:entryValues="@+array/listvalue"

您应该将其替换为:

android:entryValues="@array/listvalue"

修改
数组资源有另一个名称:

android:entryValues="@array/value"