Android偏好设置错误

时间:2011-07-15 03:47:08

标签: android preferences

在我的应用中,我正在尝试设置首选项,允许您从ListPreference中的大量选项中更改布局的背景图像。 我的偏好xml:

<PreferenceCategory android:title="Favorite Team">
    <ListPreference 
        android:title="Favorite Team" 
        android:summary="@string/prefs_pick_fav_team"
        android:key="keyFavTeam" 
        android:entries="@array/teams"
        android:entryValues="@array/teams_values" 
        android:defaultValue="0"
        />  
</PreferenceCategory>

PreferencesActivity,也在Manifest中:

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

    addPreferencesFromResource(R.xml.prefs);
}

然后这是我想要更改背景的屏幕onCreate的代码:

SharedPreferences SP = PreferenceManager
    .getDefaultSharedPreferences(getBaseContext());

    String strFavTeam = SP.getString("keyFavTeam", "0");

    LinearLayout linearLayout = (LinearLayout) this.findViewById(R.id.main_screen);

    if(strFavTeam.equals("0")){
        linearLayout.setBackgroundResource(R.drawable.first_screen);
    }
    if(strFavTeam.equals("73")){
        linearLayout.setBackgroundResource(R.drawable.tennessee_screen);
    }
    if(strFavTeam.equals("67")){
        linearLayout.setBackgroundResource(R.drawable.georgia_screen);
    }

我的问题是,当我从ListPreference中选择Tennessee(其值为73)时,我会收到一个强制关闭。

这是来自LogCat的堆栈跟踪:

07-14 23:43:35.535: ERROR/AndroidRuntime(5581): FATAL EXCEPTION: main
07-14 23:43:35.535: ERROR/AndroidRuntime(5581): java.lang.ArrayIndexOutOfBoundsException
07-14 23:43:35.535: ERROR/AndroidRuntime(5581):     at android.preference.ListPreference.onDialogClosed(ListPreference.java:218)
07-14 23:43:35.535: ERROR/AndroidRuntime(5581):     at android.preference.DialogPreference.onDismiss(DialogPreference.java:383)
07-14 23:43:35.535: ERROR/AndroidRuntime(5581):     at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1047)
07-14 23:43:35.535: ERROR/AndroidRuntime(5581):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 23:43:35.535: ERROR/AndroidRuntime(5581):     at android.os.Looper.loop(Looper.java:130)
07-14 23:43:35.535: ERROR/AndroidRuntime(5581):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-14 23:43:35.535: ERROR/AndroidRuntime(5581):     at java.lang.reflect.Method.invokeNative(Native Method)
07-14 23:43:35.535: ERROR/AndroidRuntime(5581):     at java.lang.reflect.Method.invoke(Method.java:507)
07-14 23:43:35.535: ERROR/AndroidRuntime(5581):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-14 23:43:35.535: ERROR/AndroidRuntime(5581):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-14 23:43:35.535: ERROR/AndroidRuntime(5581):     at dalvik.system.NativeStart.main(Native Method)

有人可以告诉我我需要做些什么来防止这种情况并继续改变背景?

由于

1 个答案:

答案 0 :(得分:1)

您的问题是您使用的索引大于数组的大小。来自Android源的失败的实际行是:

String value = mEntryValues[mClickedDialogEntryIndex].toString();

因此,@array/teams@array/teams_values的长度可能不匹配,或者您的值不完整。