使用prefs.xml <listpreference>进行的活动,用于启动时颜色更改崩溃</listpreference>

时间:2015-02-04 14:39:13

标签: android xml colors listpreference

我正在尝试使用SettingsActivity更改我的Main活动的背景和/或文本颜色,这是使用设置按钮打开的,并且几乎只调用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包含名称,... _值包含@android:color / ... like值和#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,它会在我尝试打开设置活动时立即崩溃。 我还没有在我的MainActivity中实现其他两个首选项列表,我想让布局首先工作。

奖金问题: 我应该如何实现背景和编辑文本颜色更改,甚至可以使用ressource链接,我可以使用

   <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")));放入从设置返回时调用的方法中,当布局工作正常时,我担心这个问题。

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)

1 个答案:

答案 0 :(得分:1)

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

<?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"/>