0

I have this small code snippet which accesses my preferences so that when my checkbox is 'true' then splash music plays. Otherwise it does not. The tutorial I am following works with this code, but mine does not. Can anyone see a problem with my code?

When I run the app, the music plays regardless but I don't get any errors.

@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();
    }

Preferences XML:

<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>

4

1 回答 1

1

更改boolean musicCheck = getPrefs.getBoolean("checkbox",true);

boolean musicCheck = getPrefs.getBoolean("Checkbox",true);

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

于 2013-03-05T19:32:02.680 回答