启动首选项活动会导致应用崩溃

时间:2017-04-25 22:05:58

标签: android

我有一个应用程序,它会在按钮单击时启动首选项活动,但会崩溃。我到处搜索,但我找不到解决办法。我的标题xml如下:

<preference-headers 
 xmlns:android="http://schemas.android.com/apk/res/android">
    <header
        android:fragment="com.example.preferencexml.preferenceFrag"
        android:title="Preference Header"
        android:summary="Preference Header Summary"/>
</preference-headers>

我的偏好xml如下:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
    android:title="Preference Category One">
    <CheckBoxPreference
        android:title="Check Box Preference"
        android:summary="Check Box Preference description"
        android:defaultValue="true"
        android:key="isChecked"/>
    </PreferenceCategory>
    <PreferenceCategory
    android:title="Preference Category Two">
    <ListPreference
        android:key="listKey"
        title="List Preference"
        android:summary="List Preference"
        android:defaultValue="one"
        android:entries="@array/entries"
        android:entryValues="@array/values"
        android:dialogTitle="Values"/>
</PreferenceCategory>
</PreferenceScreen>

我的preferenceFragment如下:

public class preferenceFrag extends PreferenceFragment 
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference);

    }
}

我的主要动作如下:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnPref=(Button)findViewById(R.id.btnPref);
        btnPref.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
            startActivityForResult(new Intent(MainActivity.this, 
  preferenceAct.class), PREF);
        }
    });
    SharedPreferences 
  prefs=PreferenceManager.getDefaultSharedPreferences(this);
    prefs.registerOnSharedPreferenceChangeListener(new 
   OnSharedPreferenceChangeListener()
    {
        public void onSharedPreferenceChanged(SharedPreferences 
 sharedPreferences, String key)
        {

        }

    });
}

0 个答案:

没有答案