Android的preferences错误 [英] Android Preferences error

查看:171
本文介绍了Android的preferences错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我试图建立preferences,允许你布局的背景图像从列表preference一吨的选择改变。
我的preferences XML:

In my app, I am trying to set up preferences that allows you to change the background image of a layout from a ton of choices in a ListPreference. My preferences 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,这也是在清单:

The PreferencesActivity, which is also in the Manifest:

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

    addPreferencesFromResource(R.xml.prefs);
}

和则这是code从我的屏幕的onCreate我想要的背景变了:

And then this is the code from my onCreate of the screen I want the background changed:

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

我的问题是,我得到一个强制关闭时,我选择田纳西(其值为73)从列表preference。

My problem is that I get a force close when I choose Tennessee (which has the value 73) from the ListPreference.

这是从LogCat中的堆栈跟踪:

This is the stack trace from the 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)

谁能告诉我什么,我需要做的prevent这与不断变化的背景继续前行?

Can someone tell me what I need to do to prevent this and move on with changing the background?

感谢

推荐答案

您的问题是,你正在使用的指数比数组的大小。从发生故障的Andr​​oid源的实际线路是这样的:

Your issue is that the index you are using is greater than the size of the array. The actual line from the Android source that is failing is this:

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

所以,你可能有一个不匹配的长度 @阵列/团队 @阵列/ teams_values​​ 或者你在那里有一个完全不正确的值。

So you probably have a mismatch in the length of the @array/teams and the @array/teams_values or you have a wholly incorrect value in there.

这篇关于Android的preferences错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆