I'm trying to change background and/or text colours of my Main activity with a SettingsActivity which is opened with the settings button and pretty much only calls addPreferencesFromResource(R.xml.preferences). This is the SettingsActivity:
public class SettingsActivity extends PreferenceActivity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
I have string-arrays in an arrays.xml text_color_entries holding names, ..._values holding @android:color/... like values and an #rrggbb value:
<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>
And a preferences.xml holding a CheckboxPreference and 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>
When I only had the CheckboxPreference and the first ListPreference in there, it worked perfectly fine. It also works fine when I comment out the other 2 Listpreferences. But as soon I have at least one of the other ListPreferences in the app it crashes as soon as I try to open the settings activity. I haven't implemented those other two preference lists in my MainActivity yet, I wanted to get the layout to work first.
Bonus question: How exactly should I implement the background and edittext-text color changing, is it even possible using the ressource links, and can I use a
<color>
array instead?
Thanks for your time, I hope that's not too much stuff up there.
Greetings, Marlon
EDIT: Tried to debug, didn't really help me. Went through some deeper classes I don't know about and then crashed. Also, LogCat says there is a NullPointerException somewhere, but doesn't give me a concrete position in my code, only points to
ListPreference.findiIndexOfValue(Listpreference.java:223)
Edit 2: esme-louise pointed out my value arrays were missing a line. I changed that, it unfortunately didn't solve the problem.
Edit 3: color.xml now looks like Audi describes, ListPrefrence now has android:entries="@array/background_colors"
android:entryValues="@array/background_colors"
but still crashes the app when starting the settings activity.
Edit 4: I now put notesEditText.setTextColor(Color.parseColor(sharedPreferences.getString ("pref_background_colour", "@color/OriginalBackground")));
into the method called when returning from the settings, worrying about that when the Layout thing works.
The 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)
Try using a color array instead of a string array. It is something like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#000000</color>
<color name="white">#FFFFFF</color>
</resources>
Add this to your 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>
Modify your ListPreference like this
<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"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.