我正在尝试使用SettingsActivity更改我的主活动的背景和/或文本颜色,该设置通过设置按钮打开,并且几乎只调用addPreferencesFromResource(R.xml.preferences)。
这是SettingsActivity:
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>
还有一个Preferences.xml,其中包含一个CheckboxPreference和3个ListPreferences:
<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时,它工作得很好。当我注释掉其他两个Listpreferences时,它也可以正常工作。
但是,一旦我在应用程序中至少有一个其他ListPreferences,当我尝试打开设置活动时,它就会崩溃。
我尚未在MainActivity中实现其他两个首选项列表,我想让布局首先生效。
奖励问题:
我应该如何准确地实现背景和edittext-text颜色的更改,甚至可以使用资源链接,并且我可以使用
<color>
代替数组?
感谢您的宝贵时间,我希望那里没有太多东西。
问候,
马龙
编辑:试图调试,并没有真正帮助我。经历了一些我不了解的更深层次的课程,然后崩溃了。
此外,LogCat表示某个地方存在NullPointerException,但没有在代码中给我具体的位置,仅指向
ListPreference.findiIndexOfValue(Listpreference.java:223)
编辑2:esme-louise指出我的值数组缺少一行。我更改了它,不幸的是没有解决问题。
编辑3:color.xml现在看起来像奥迪描述的那样,ListPrefrence现在有了
android:entries="@array/background_colors"
android:entryValues="@array/background_colors"
但在启动设置活动时仍会使应用程序崩溃。
编辑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"/>