Android 偏好设置错误

发布于 2024-11-24 04:05:32 字数 2913 浏览 1 评论 0原文

在我的应用程序中,我尝试设置首选项,允许您从 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,也在清单中:

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

我的问题是,当我选择田纳西州(其值为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)

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的味道 2024-12-01 04:05:32

您的问题是您使用的索引大于数组的大小。 Android 源代码中失败的实际行是这样的:

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

所以您可能在 @array/teams@array/teams_values 的长度上不匹配,或者您那里有一个完全错误的值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文