开发者

Can't get item from preferences in Android

开发者 https://www.devze.com 2023-03-16 04:26 出处:网络
I\'ve got a preferences.xml stored in my res/xml folder: <?xml version=\"1.0\" encoding=\"utf-8\"?>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消