Can't get item from preferences in Android
I've got a preferences.xml stored in my res/xml folder:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory>
<Preference android:key="units_length" android:title="imperial"></Preference>
</PreferenceCategory>
</PreferenceScreen>
Here is how the onCreate method looks like in my activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.results);
// Get preferences
SharedPreferences preferences = getSharedPreferences("preferences", 0);
// Fetch the text view
TextView text = (TextView) findViewById(R.id.textView1);
// Set new text
text.setText(preferences.getString("uni开发者_运维知识库ts_length", "nothing"));
}
My application just says "nothing". What am I doing wrong here?
Try setting android:defaultValue
attribute in your preference xml definition to whatever appropriate. It seems that property have never been initialized.
精彩评论