首选项管理器未从用户首选项中获取价值
Preference Manager not getting value from user preferences
我正在制作一个应用程序,无论何时你去 activity,它都会播放一个声音文件。我做了一个偏好,您可以在其中更改它播放的声音文件。这是首选项 activity 的代码:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference android:title="Sound"
android:summary="Pick the sound!"
android:key="soundChoice"
android:defaultValue="a"
android:entries="@array/soundList"
android:entryValues="@array/soundValues" />
</PreferenceScreen>
这里是数组文件内容:
<string-array name="soundList">
<item>Sound 1</item>
<item>Sound 2</item>
<item>Sound 3</item>
<item>Sound 4</item>
</string-array>
<string-array name="soundValues">
<item>a</item>
<item>b</item>
<item>c</item>
<item>d</item>
</string-array>
这是播放声音文件的 Activity 的 onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
//Have also tried using "this" instead of "getBaseContext(). Same result.
String choiceSound = SP.getString("soundChoice","a");
// TODO
if(choiceSound == "a"){
mediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mediaPlayer.start();
}
else if(choiceSound == "b") {
mediaPlayer = MediaPlayer.create(this, R.raw.sound2);
mediaPlayer.start();
}
setContentView(R.layout.activity_sounder);
}
我的问题是,SharedPreferences 没有从用户选择的任何内容中获取值。
所以媒体播放器永远不会被创建,导致应用程序在我退出 activity 时崩溃,因为我在 onStop() 方法中释放了 MediaPlayer。
我的 SharedPreference/PreferenceManager 构造函数是否正确?
关于我做错了什么或如何修复它的任何想法?
尝试使用 equals() 而不是 == 检查字符串。
if(choiceSound.equals("a"))
而不是
if(choiceSound == "a")
'=='比较对象的引用。你想检查它的价值。
我正在制作一个应用程序,无论何时你去 activity,它都会播放一个声音文件。我做了一个偏好,您可以在其中更改它播放的声音文件。这是首选项 activity 的代码:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference android:title="Sound"
android:summary="Pick the sound!"
android:key="soundChoice"
android:defaultValue="a"
android:entries="@array/soundList"
android:entryValues="@array/soundValues" />
</PreferenceScreen>
这里是数组文件内容:
<string-array name="soundList">
<item>Sound 1</item>
<item>Sound 2</item>
<item>Sound 3</item>
<item>Sound 4</item>
</string-array>
<string-array name="soundValues">
<item>a</item>
<item>b</item>
<item>c</item>
<item>d</item>
</string-array>
这是播放声音文件的 Activity 的 onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
//Have also tried using "this" instead of "getBaseContext(). Same result.
String choiceSound = SP.getString("soundChoice","a");
// TODO
if(choiceSound == "a"){
mediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mediaPlayer.start();
}
else if(choiceSound == "b") {
mediaPlayer = MediaPlayer.create(this, R.raw.sound2);
mediaPlayer.start();
}
setContentView(R.layout.activity_sounder);
}
我的问题是,SharedPreferences 没有从用户选择的任何内容中获取值。
所以媒体播放器永远不会被创建,导致应用程序在我退出 activity 时崩溃,因为我在 onStop() 方法中释放了 MediaPlayer。
我的 SharedPreference/PreferenceManager 构造函数是否正确?
关于我做错了什么或如何修复它的任何想法?
尝试使用 equals() 而不是 == 检查字符串。
if(choiceSound.equals("a"))
而不是
if(choiceSound == "a")
'=='比较对象的引用。你想检查它的价值。