开发者

Android Preferences error

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>

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

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

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

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)

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

Thanks


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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜