Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 首选项不起作用_Java_Android - Fatal编程技术网

Java 首选项不起作用

Java 首选项不起作用,java,android,Java,Android,我有一个小的代码片段,可以访问我的首选项,这样当我的复选框为“true”时,就会播放splash音乐。否则它就不会。我下面的教程适用于此代码,但我的不适用。有人看到我的代码有问题吗 当我运行应用程序时,音乐会播放,但我没有收到任何错误 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstan

我有一个小的代码片段,可以访问我的首选项,这样当我的复选框为“true”时,就会播放splash音乐。否则它就不会。我下面的教程适用于此代码,但我的不适用。有人看到我的代码有问题吗

当我运行应用程序时,音乐会播放,但我没有收到任何错误

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash);

    ourSong = MediaPlayer.create(Splash.this, R.raw.clip1);

    SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean musicCheck = getPrefs.getBoolean("checkbox",true);
    if (musicCheck == true){
        ourSong.start();
    }
首选项XML:



更改
boolean musicCheck=getPrefs.getBoolean(“复选框”,true)

boolean musicCheck=getPrefs.getBoolean(“复选框”,true)


我相信您获得的首选项的名称需要与首选项的键匹配。

此处的默认值为true。因此,如果您没有正确编辑该值,它将始终为真。显示将复选框值保存到SharedReferences的代码。首选项的名称实际上是“复选框”吗?如果您没有该名称的首选项,则默认为true,因此我猜该首选项不存在,您在某个地方将其命名错误,因此您获得了默认值。显示您的首选项xml,请
“Checkbox”!=“复选框”
请帮您的用户一个忙。如果有什么比闪屏更糟糕的话,那就是播放音乐的闪屏。除非我真的需要,否则卸载一个应用程序就足够了。
<EditTextPreference
    android:title="EditText"
    android:key="name"
    android:summary="Enter your name"/>

<CheckBoxPreference
    android:title="Music"
    android:defaultValue="True"
    android:key="Checkbox"
    android:summary="For the Splash Screen"/>

<ListPreference
    android:title="List"
    android:key="list"
    android:summary="This is a list"
    android:entries="@array/list"
    android:entryValues="@array/lvalues"
    ></ListPreference>