当前位置: 首页 > 知识库问答 >
问题:

启动时使用prefs.xml进行颜色变化崩溃的活动

顾淳
2023-03-14
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/...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>

和3个ListPreferences.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时,它工作得非常好。当我注释掉另外两个ListPreferences时,它也很好。但当我在应用程序中至少有一个其他ListPreferences时,当我尝试打开设置活动时,它就会崩溃。我还没有在我的MainActivity中实现其他两个首选项列表,我想先让布局工作起来。

附加问题:我应该如何实现背景和edittext-text颜色变化,甚至可以使用ressource链接吗,我可以使用

   <color>  

而是数组?

谢谢你的时间,我希望上面的东西不会太多。

编辑:尝试调试,没有真正帮助我。上了一些我不知道的更深的课程然后就崩溃了。而且,LogCat说某处有一个NullPointerException,但没有给出代码中的具体位置,只指向
ListPreference.FindiIndexOfValue(ListPreference.java:223)

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

Edit3:color.xml现在看起来像Audi描述的那样,ListPrefrence现在有android:entries=“@array/background_colors”
android:entryvalues=“@array/background_colors”,但在启动设置活动时仍然会使应用程序崩溃。

编辑4:我现在将NotesEditText.SettextColor(Color.Parsecolor(SharedPreferences.GetString(“pref_background_color”,“@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个答案

越新霁
2023-03-14

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

<?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"/>
 类似资料:
  • 我正在创建一个应用程序, 在发射时崩溃了,并说 以下是完整的错误日志: 致命异常:main 10-16 15:56:26.135 253 42-25342/com.social_infotech.renthouse e/androidruntime:java.lang.runtimeException:无法启动活动ComponentInfo{com.social_infotech.renthous

  • 我已经配置了我的日食(开普勒)IDE来开发亚马逊云应用程序和Android应用程序。当我重新启动它以反映更改时,它不会启动。日食窗口在选择工作区后自动关闭。早些时候,它在开发简单的Java应用程序时运行良好。 我的eclipse.ini文件如下所示 我认为是我的eclipse.ini文件中的某个东西迫使eclipse退出。PS。我使用的是ubuntu Linux 12.04

  • 我有一个问题。在过去的4-5周里,我一直在Eclipse中使用Android,并且虚拟机从未出现过问题(是的,它们很慢。但从未崩溃)。 现在,我相信,自从4.2开始为uni开发一个新项目(截止日期下周)以来,我的4.2虚拟机在启动时突然开始崩溃,因为“内核恐慌”(最后的响应)。 我想问一下我是否有什么可以做的,也许可以下载修补程序或其他什么。API和Eclipse都是最新的,或者我的计算机可能有什

  • 问题是,当我在Google中使用授权运行此代码时,它会立即崩溃: 但是日志中的错误: 2019-06-22 17:55:42.652 29327-23222/?E/AudioSource:在已关闭的AudioSource 2019-06-22 17:55:43.240 2093-2616/?E/TouchFilter:setTouchFilter日志启用参数:0 2019-06-22 17:55:

  • 我们正在为android开发一个独立游戏,并希望用户选择他的昵称。我们选择使用NDK提供的本机活动,因为这似乎是最简单的方法。 我们遇到的键盘的第一个问题是函数ANativeActivity_showSoftInput()似乎什么都不做(如这里所述),所以我们使用JNI调用来启动键盘: 这对于调出键盘很好,在一些设备上也很好。但在其他设备上(例如Nexus7),当用户试图通过单击“隐藏键盘”按钮关