当我尝试在SettingsFragment中找到首选项时,我总是得到NULL。

设置片段

public static class SettingsFragment extends PreferenceFragment {

public final String KEY_PREF_SERVER_IP = "key_pref_server_ip";

@Override
public void onCreate(Bundle savedInstanceState) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getAppContext());

    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

    SharedPreferences.OnSharedPreferenceChangeListener spChanged = new
            SharedPreferences.OnSharedPreferenceChangeListener() {
                @Override
                public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                                      String key) {
                    if(key.equals(KEY_PREF_SERVER_IP) || key.equals(KEY_PREF_SERVER_PORT)) {

                        System.out.println(key);
                        EditTextPreference muffin = (EditTextPreference) SettingsFragment.findPreference(key);

                    }

                    change = 1;
                }


            };
    prefs.registerOnSharedPreferenceChangeListener(spChanged);
}


}

因此,当我尝试EditTextPreference muffin = (EditTextPrefernce) new SettingsFragment.findPreference(key)时,我总是得到NULL。

诀窍是什么?

编辑:

现在,整个代码如下:

public class Einstellungen extends Activity{

    private static Context context;


    public static final String KEY_PREF_SERVER_IP =  "key_pref_server_ip";
    public static final String KEY_PREF_SERVER_PORT =  "key_pref_server_port";
    public static int change = 0;

    public static class SettingsFragment extends PreferenceFragment {



        public final String KEY_PREF_SERVER_IP = "key_pref_server_ip";

        @Override
        public void onCreate(Bundle savedInstanceState) {

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getAppContext());

            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);

            SharedPreferences.OnSharedPreferenceChangeListener spChanged = new
                    SharedPreferences.OnSharedPreferenceChangeListener() {
                        @Override
                        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                                              String key) {
                            if(key.equals(KEY_PREF_SERVER_IP) || key.equals(KEY_PREF_SERVER_PORT)) {

                                System.out.println(key);
                                EditTextPreference muffin = (EditTextPreference) SettingsFragment.findPreference(key);

                            }

                            change = 1;
                        }


                    };
            prefs.registerOnSharedPreferenceChangeListener(spChanged);
        }

    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Einstellungen.context = getApplicationContext();

                super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        Einstellungen.context = this;




        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getFragmentManager().beginTransaction()
                .replace(R.id.test, new SettingsFragment())
                .commit();

        getFragmentManager().executePendingTransactions();

    }

    public static Context getAppContext() {
        return Einstellungen.context;
    }



    public void onBackPressed() {

        if(change == 1) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(Einstellungen.this, "Um die Änderungen zu übernehmen bitte die App neustarten", Toast.LENGTH_SHORT).show();
                }
            });
        }

        finish();

    }



}


preferences.xml

<?xml version="1.0" encoding="utf-8"?>





    android:title="@string/Servereinstellungen"
    android:key="key_pref_server">
    <EditTextPreference
        android:title="@string/IP_Adresse"
        android:key="key_pref_server_ip"
        android:summary="(IP)"
        android:inputType="number"
        android:defaultValue="(IP)"/>
    <EditTextPreference
        android:title="@string/Port"
        android:key="key_pref_server_port"
        android:inputType="number"
        android:summary="(PORT)"
        android:defaultValue="(PORT)"/>
</PreferenceCategory>
<PreferenceCategory
    android:title="ÜBER"
    android:descendantFocusability="blocksDescendants"
    >
    <EditTextPreference
        android:title="Entwickler"
        android:summary=""
        android:selectable="false"
     />
    <EditTextPreference
        android:title="Version"
        android:summary="1.0"
        android:selectable="false"
        />

</PreferenceCategory>



我想我在某个地方犯了一个错误:)

最佳答案

您必须更改以下内容:


EditTextPreference松饼=(EditTextPrefernce)新的SettingsFragment.findPreference(key)


至:


EditTextPreference松饼=(EditTextPrefernce)SettingsFragment.findPreference(key)


您只想搜索当前的Fragment,而不要创建一个新的Fragment。

07-27 21:50