2

Could someone maybe tell me what i'm doing wrong? I'm betting im missing one small thing. I've looked on the developer site and i've read some tutorials and i'm just not seeing what i did wrong.

I'm trying to use a ListPreference to decide which sound to play on a button click.

I have this at the top:

public String greensound;

Here's my OnClick code:

case R.id.green:
     SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(this);
     greensound  = prefs.getString("greensound", "gsone");
       if (greensound == "gsone") {
        mSoundManager.playSound(1); 
       } else if (greensound == "gstwo") {
        mSoundManager.playSound(2); 
       } else if (greensound == "gsthree") {
        mSoundManager.playSound(3);
       }
 break;

Here's my xml:

<ListPreference 
android:title="Geen Button" 
android:key="greensound"
android:summary="Select sound for the Green Button" 
android:entries="@array/green_list" 
android:entryValues="@array/green_list_values"
android:defaultValue="gsone">
</ListPreference>

here's my Settings.java:

package com.my.app;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Settings extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);


    }


}

and here's my array's if that will help at all:

//This is the one I want to display to the user
    <string-array name="green_list"> 
      <item>Sound One</item>
      <item>Sound Two</item>
      <item>Sound Three</item>
      <item>Sound Four</item>
      <item>Sound Five</item>
    </string-array>


    <string-array name="green_list_values"> 
      <item>gsone</item>
      <item>gstwo</item>
      <item>gsthree</item>
      <item>gsfour</item>
      <item>gsfive</item>
    </string-array>

edit: added a logcat that kinda looked possibly related.

08-27 01:52:07.738: WARN/Resources(6846): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f090000}
08-27 01:52:07.748: WARN/Resources(6846): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f090000}
08-27 01:52:07.758: WARN/Resources(6846): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f090000}

DDMS > File Explorer > Data > Data > packageName > SharedPreferences This is what was in there:

com.my.app_preferences.xml:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="redsound">rsone</string>
<string name="greensound">gsone</string>
</map>

_has_set_default_values.xml:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="_has_set_default_values" value="true" />
</map>

This all really confuses me more because...It looks like greedsound does infact = gsone so.... I don't understand whats wrong its not even playing the default sound. and yes i've tested

mSoundManager.playSound(1); 
mSoundManager.playSound(2); 
mSoundManager.playSound(3);

all without the other code and they work great. I'm not sure what's work

4

3 回答 3

1

greensound.equals("gsone")

于 2010-08-28T04:07:34.797 回答
1

我有一个类似的问题。我将我的 '==' 比较更改为 string.contentsEquals() 并且事情开始起作用了。我最终将键和值放入 HashMaps。

于 2010-09-05T03:42:05.263 回答
0

我能想到的唯一问题是在运行 playSound 代码之前没有设置您的偏好。为确保加载设置,在 onCreate() 中包含以下代码:

/* Loading default preferences the first time application is run */
        PreferenceManager.setDefaultValues(getApplicationContext(),
                R.xml.filename, false);

此外,通过 DDMS > File Explorer > Data > Data > packageName > SharedPreferences 检查您的首选项是否已设置。

当您使用 Preference Activity 并从 xml 资源创建它时。它会自动创建一个 SharedPreference 文件:packageName_preferences(例如 com.my_company.my_app_preferences)。因此,要访问它,您需要使用以下代码:

SharedPreferences prefs = getSharedPreferences("com.my.app_preferences", MODE_PRIVATE);

最后删除 xml 中的以下行:

android:defaultValue="gsone"

希望这可以帮助。

于 2010-08-27T04:11:24.827 回答