DayNightTheme适用于一个应用程序而不是另一个应用程序?

时间:2016-03-12 01:32:14

标签: java android

我有一个非常莫名其妙的时间试图解决这个主题问题。我有两个独立的应用程序,它们都具有相同的样式xml,夜间颜色,而不是夜间颜色文件。他们都有着色的矢量绘图作为drawable。事情就是一个人崩溃,一个人没有。还应该注意的是,该应用程序仅在Android 5.0上崩溃。以上任何工作和kitkat似乎都有效。

以下是崩溃报告:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nick.mowen.materialdesignplugin/com.nick.mowen.materialdesign.ui.MainActivity}: java.lang.UnsupportedOperationException: Can't convert to color: type=0x1
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
       at android.app.ActivityThread.access$800(ActivityThread.java:151)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5254)
       at java.lang.reflect.Method.invoke(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x1
       at android.content.res.TypedArray.getColor(TypedArray.java:404)
       at android.app.Activity.onApplyThemeResource(Activity.java:3683)
       at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:140)
       at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:85)
       at android.support.v7.app.AppCompatActivity.setTheme(AppCompatActivity.java:89)
       at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:81)
       at com.nick.mowen.materialdesign.ui.MainActivity.onCreate(MainActivity.java:85)
       at android.app.Activity.performCreate(Activity.java:5990)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
       at android.app.ActivityThread.access$800(ActivityThread.java:151) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:135) 
       at android.app.ActivityThread.main(ActivityThread.java:5254) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:372) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

这是导致崩溃的夜间颜色xml - 有趣的是,当设置为#212121时导航抽屉颜色导致崩溃,但是当它设置为@android时:颜色/黑色它工作正常:

<resources>>

    <color name="color_primary_dark">#212121</color>
    <color name="accent_primary">@color/accent_material_dark</color>
    <color name="back_primary">#FF303030</color>
    <color name="list_selected_primary">@color/accent_material_dark_selected</color>
    <color name="text_third">@android:color/tertiary_text_dark</color>
    <color name="text_second">@android:color/secondary_text_dark</color>
    <color name="text_main">@android:color/primary_text_dark</color>
    <color name="drawable_tint">@null</color>
    <color name="dialog_back">#424242</color>
    <color name="action_primary">#212121</color>
    <color name="inverse_text_third">@android:color/tertiary_text_light</color>
    <color name="inverse_text_second">@android:color/secondary_text_light</color>
    <color name="inverse_text_main">@android:color/primary_text_light</color>
    <bool name="dark_status">false</bool>
    <color name="navigation_primary">#212121</color>

</resources>

1 个答案:

答案 0 :(得分:2)

我看了一下AOSP的5.0代码(http://androidxref.com/5.0.0_r2/xref/frameworks/base/core/java/android/app/Activity.java

这是被调用的方法:

@Override
protected void onApplyThemeResource(Resources.Theme theme, int resid,
        boolean first) {
    if (mParent == null) {
        super.onApplyThemeResource(theme, resid, first);
    } else {
        try {
            theme.setTo(mParent.getTheme());
        } catch (Exception e) {
            // Empty
        }
        theme.applyStyle(resid, false);
    }

    // Get the primary color and update the TaskDescription for this activity
    if (theme != null) {
        TypedArray a = theme.obtainStyledAttributes(com.android.internal.R.styleable.Theme);
        int colorPrimary = a.getColor(com.android.internal.R.styleable.Theme_colorPrimary, 0);
        a.recycle();
        if (colorPrimary != 0) {
            ActivityManager.TaskDescription v = new ActivityManager.TaskDescription(null, null,
                    colorPrimary);
            setTaskDescription(v);
        }
    }
}

崩溃的行是getColor。您为特定主题指定的颜色似乎无法解析为颜色。检查您的资源,并通过API (TypedArray).getColor()确认可以将其解析为一种颜色:

注意它对错误的说法:

  

UnsupportedOperationException如果属性已定义但未定义   整数颜色颜色状态列表