gpt4 book ai didi

android - 使用 prefs.xml 进行颜色更改的 Activity 在启动时崩溃

转载 作者:行者123 更新时间:2023-11-30 02:22:11 27 4
gpt4 key购买 nike

我正在尝试使用 SettingsActivity 更改我的主要 Activity 的背景和/或文本颜色,该 Activity 通过设置按钮打开并且几乎只调用 addPreferencesFromResource(R.xml.preferences)。这是设置 Activity :

public class SettingsActivity extends PreferenceActivity {

@SuppressWarnings("deprecation")

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}

我在 arrays.xml 中有字符串数组 text_color_entries 保存名称,..._values 保存 @android:color/... 类似值和 #rrggbb 值:

<resources>

<string-array name="text_size_entries">
<item>16 pt</item>
<item>18 pt</item>
<item>20 pt</item>
<item>22 pt</item>
<item>24 pt</item>
<item>26 pt</item>
</string-array>
<string-array name="text_size_values">
<item>16</item>
<item>18</item>
<item>20</item>
<item>22</item>
<item>24</item>
<item>26</item>
</string-array>
<string-array name="background_colour_entries">
<item>Original</item>
<item>White</item>
<item>Light Orange</item>
<item>Dark Orange</item>
<item>Light Green</item>
<item>Dark Green</item>
<item>Light Red</item>
<item>Dark Red</item>
<item>Bright Blue</item>
<item>Light Blue</item>
<item>Dark Blue</item>
<item>Black</item>
</string-array>
<string-array name="background_colour_values">
<item>#0030ff</item>
<item>@android:color/background_light</item>
<item>@android:color/holo_orange_dark</item>
<item>@android:color/holo_green_light</item>
<item>@android:color/holo_green_dark</item>
<item>@android:color/holo_red_light</item>
<item>@android:color/holo_red_dark</item>
<item>@android:color/holo_blue_bright</item>
<item>@android:color/holo_blue_light</item>
<item>@android:color/holo_blue_dark</item>
<item>@android:color/background_dark</item>
</string-array>
<string-array name="text_colour_entries">
<item>Original(Light Orange)</item>
<item>White</item>
<item>Light Orange</item>
<item>Dark Orange</item>
<item>Light Green</item>
<item>Dark Green</item>
<item>Light Red</item>
<item>Dark Red</item>
<item>Bright Blue</item>
<item>Light Blue</item>
<item>Dark Blue</item>
<item>Black</item>
</string-array>
<string-array name="text_colour_values">
<item>@android:color/holo_orange_light</item>
<item>@android:color/background_light</item>
<item>@android:color/holo_orange_dark</item>
<item>@android:color/holo_green_light</item>
<item>@android:color/holo_green_dark</item>
<item>@android:color/holo_red_light</item>
<item>@android:color/holo_red_dark</item>
<item>@android:color/holo_blue_bright</item>
<item>@android:color/holo_blue_light</item>
<item>@android:color/holo_blue_dark</item>
<item>@android:color/background_dark</item>
</string-array>
</resources>

还有一个包含 CheckboxPreference 和 3 个 ListPreferences 的 preferences.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Settings">

<CheckBoxPreference
android:key="pref_text_bold"
android:title="Make Text Bold"
android:summary="Makes the note text bold"
android:defaultValue="false"/>

<ListPreference
android:key="pref_text_size"
android:title="Text Size"
android:summary="Pick the text size"
android:entries="@array/text_size_entries"
android:entryValues="@array/text_size_values"
android:defaultValue="16"/>
<!-- Works if I erase/comment out anything below here -->
<ListPreference
android:key="pref_background_colour"
android:title="Background colour"
android:summary="Choose the background colour of the app"
android:entries="@array/background_colour_entries"
android:entryValues="@array/background_colour_values"
android:defaultValue="#0030ff"/>

<ListPreference
android:key="pref_text_colour"
android:title="Text colour"
android:summary="Choose the colour of your text"
android:entries="@array/text_colour_entries"
android:entryValues="@array/text_colour_values"
android:defaultValue="@android:color/holo_orange_light"/>

</PreferenceScreen>

当我只有 CheckboxPreference 和第一个 ListPreference 时,它​​工作得很好。当我注释掉其他 2 个 Listpreferences 时,它也能正常工作。但是只要我在应用程序中至少有一个其他 ListPreferences,它就会在我尝试打开设置 Activity 时崩溃。我还没有在我的 MainActivity 中实现其他两个首选项列表,我想先让布局工作。

奖金问题:我到底应该如何实现背景和 edittext-text 颜色变化,甚至可以使用资源链接,我可以使用

   <color>  

数组代替?

感谢您抽出时间,我希望上面的内容不会太多。

您好,马龙

编辑:试图调试,并没有真正帮助我。经历了一些我不知道的更深层次的类(class),然后崩溃了。另外,LogCat 说某处有一个 NullPointerException,但没有给我代码中的具体位置,只指向
ListPreference.findiIndexOfValue(Listpreference.java:223)

编辑 2:esme-louise 指出我的值数组缺少一行。我改变了它,不幸的是它没有解决问题。

编辑 3:color.xml 现在看起来像奥迪描述的那样,ListPrefrence 现在有 android:entries="@array/background_colors"
android:entryValues="@array/background_colors"但在启动设置 Activity 时仍然会导致应用程序崩溃。

编辑 4:我现在把 notesEditText.setTextColor(Color.parseColor(sharedPreferences.getString
("pref_background_colour", "@color/OriginalBackground")));
进入从设置返回时调用的方法,在 Layout 工作时担心这一点。

LogCat:

02-08 16:07:25.395  23749-23749/com.regenhardt.savingstate E/﹕ appName=com.regenhardt.savingstate, acAppName=/system/bin/surfaceflinger
02-08 16:07:25.395 23749-23749/com.regenhardt.savingstate E/﹕ 0
02-08 16:07:29.357 23749-23749/com.regenhardt.savingstate E/TextView﹕ get resource from application failed.
**This one comes many, many times here and before the surfaceflinger thing, practically all the time it's running. I deleted a TextView from the layout and commented it out of the java code; wasn't anything dynamic, just a title**
02-08 16:07:30.932 23749-23749/com.regenhardt.savingstate E/TextView﹕ get resource from application failed.
02-08 16:07:30.952 23749-23749/com.regenhardt.savingstate E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.regenhardt.savingstate, PID: 23749
java.lang.NullPointerException
at android.preference.ListPreference.findIndexOfValue(ListPreference.java:223)
**From here on stuff I couldn't even begin to understand so didn't bother digging into android**
at android.preference.ListPreference.getValueIndex(ListPreference.java:232)
at android.preference.ListPreference.getEntry(ListPreference.java:210)
at android.preference.ListPreference.getSummary(ListPreference.java:156)
at android.preference.Preference.onBindView(Preference.java:534)
at android.preference.Preference.getView(Preference.java:472)
at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221)
at android.widget.AbsListView.obtainView(AbsListView.java:2338)
at android.widget.ListView.makeAndAddView(ListView.java:1812)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1645)
at android.widget.AbsListView.onLayout(AbsListView.java:2149)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1877)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1653)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2317)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2023)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

尝试使用颜色数组而不是字符串数组。是这样的

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#000000</color>
<color name="white">#FFFFFF</color>
</resources>

将此添加到您的 strings.xml

<string-array name="backgroud_entries">
<item>Black</item>
<item>White</item>
</string-array>

<string-array name="background_entry_values">
<item>@color/black</item>
<item>@color/white</item>
</string-array>

像这样修改您的 ListPreference

<ListPreference
android:key="pref_background_colour"
android:title="Background colour"
android:summary="Choose the background colour of the app"
android:entries="@array/background_entries"
android:entryValues="@array/background_entry_values"
android:defaultValue="#0030ff"/>

关于android - 使用 prefs.xml <ListPreference> 进行颜色更改的 Activity 在启动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28324044/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com