PreferenceFragment为空(Android)

时间:2016-06-01 09:40:44

标签: android android-preferences

当我启动应用时,屏幕上没有显示

Checking duplicate files against a dictionary of filesizes and names

我尝试扩展PreferenceActivity而不是AppCompactActivity,但它没有起作用。

MainActivity中的代码是

package com.hamza.testing;

import android.app.Activity;
import android.os.PersistableBundle;
import android.preference.PreferenceFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        if (savedInstanceState == null)
            getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingFragment()).commit();
    }

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

}

xml / generel_prefs.xml中的代码是

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <ListPreference
        android:defaultValue="0"
        android:entries="@array/unit_entries"
        android:entryValues="@array/unit_entry_values"
        android:key="unit"
        android:title="Unit" />
    <EditTextPreference
        android:title="Zip Code"
        android:key="zipcode"
        android:defaultValue="72345"
        />

</PreferenceScreen>

并且values / array.xml中的代码是

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="unit_entries">
        <item>Metric</item>
        <item>Imperial</item>
    </string-array>
    <string-array name="unit_entry_values">
        <item>0</item>
        <item>1</item>
    </string-array>
</resources>

由于

1 个答案:

答案 0 :(得分:4)

我用过

错误的onCreate方法 我用过这个

public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

而不是

protected void onCreate(Bundle savedInstanceState)