当我尝试在SettingsFragment中找到一个Preference时,我总是得到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>
我想我做错了某事:)
答案 0 :(得分:1)
您必须更改以下内容:
EditTextPreference muffin =(EditTextPrefernce)new SettingsFragment.findPreference(key)
要:
EditTextPreference muffin =(EditTextPrefernce)SettingsFragment.findPreference(key)
您只想搜索当前的片段而不是创建新的片段。
答案 1 :(得分:0)
如果其他人有问题,这是解决方案:
EditTextPreference muffin =(EditTextPreference)SettingsFragment.this.findPreference(key);