创建自定义样式崩溃的应用程序 [英] Creating custom style crashes app

查看:220
本文介绍了创建自定义样式崩溃的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立我的应用程序,以便它可以从多个主题中受益。

I am building my app so that it can benefit from multiple themes.

我已经得到了我想要的添加自定义项目如舞台

I have got to the stage where I want to add custom items e.g.

<style name="My.Theme.Default">
...
    <item name="borderColorDialog">@color/red</item>
...
</style>

<style name="My.Theme.Blue">
...
    <item name="borderColorDialog">@color/blue</item>
...
</style>

所以,我已经添加属性 borderColorDialog 来我的ATTR文件,如下所示:

So I've added the attribute borderColorDialog to my attr file as follows:

<attr name="borderColorDialog" format="color" />

现在我想对它进行测试。我有一个绘制如下:

Now I want to test it. I have a drawable as follows:

<?xml version="1.0" encoding="utf-8"?>
<layer-list 
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>

        <shape 
            android:shape="rectangle" >

            <solid 
                android:color="?borderColorDialog" />

        </shape>

    </item> 

    <item
        android:bottom="2dp">

        <shape 
            android:shape="rectangle" >

            <solid 
                android:color="@color/black" />

        </shape>

    </item>
</layer-list>

这一切似乎在逻辑上的声音给我,但是 borderColorDialog 引用崩溃模拟器:

This all appears logically sound to me, however the ?borderColorDialog reference crashes the emulator:

E/AndroidRuntime(1461): FATAL EXCEPTION: main
E/AndroidRuntime(1461): java.lang.RuntimeException: Unable to start activity ComponentInfo{.MainActivity}: android.view.InflateException: Binary XML file line #26: Error inflating class com.android.internal.widget.ActionBarContainer
E/AndroidRuntime(1461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
E/AndroidRuntime(1461):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
E/AndroidRuntime(1461):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
E/AndroidRuntime(1461):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
E/AndroidRuntime(1461):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(1461):     at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(1461):     at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime(1461):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1461):     at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(1461):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime(1461):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(1461):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(1461): Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class com.android.internal.widget.ActionBarContainer
E/AndroidRuntime(1461):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
E/AndroidRuntime(1461):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
E/AndroidRuntime(1461):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E/AndroidRuntime(1461):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime(1461):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(1461):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime(1461):     at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2784)
E/AndroidRuntime(1461):     at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2844)
E/AndroidRuntime(1461):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:252)
E/AndroidRuntime(1461):     at android.app.Activity.setContentView(Activity.java:1867)
E/AndroidRuntime(1461):     at .MainActivity.onCreate(MainActivity.java:30)
E/AndroidRuntime(1461):     at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime(1461):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime(1461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
E/AndroidRuntime(1461):     ... 11 more
E/AndroidRuntime(1461): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime(1461):     at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime(1461):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
E/AndroidRuntime(1461):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
E/AndroidRuntime(1461):     ... 24 more
E/AndroidRuntime(1461): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/actionbar_background.xml from drawable resource ID #0x7f020001
E/AndroidRuntime(1461):     at android.content.res.Resources.loadDrawable(Resources.java:1918)
E/AndroidRuntime(1461):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
E/AndroidRuntime(1461):     at android.view.View.<init>(View.java:3336)
E/AndroidRuntime(1461):     at android.view.ViewGroup.<init>(ViewGroup.java:427)
E/AndroidRuntime(1461):     at android.widget.FrameLayout.<init>(FrameLayout.java:101)
E/AndroidRuntime(1461):     at android.widget.FrameLayout.<init>(FrameLayout.java:97)
E/AndroidRuntime(1461):     at com.android.internal.widget.ActionBarContainer.<init>(ActionBarContainer.java:52)
E/AndroidRuntime(1461):     ... 27 more
E/AndroidRuntime(1461): Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
E/AndroidRuntime(1461):     at android.content.res.TypedArray.getColor(TypedArray.java:326)
E/AndroidRuntime(1461):     at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:951)
E/AndroidRuntime(1461):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
E/AndroidRuntime(1461):     at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:165)
E/AndroidRuntime(1461):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
E/AndroidRuntime(1461):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
E/AndroidRuntime(1461):     at android.content.res.Resources.loadDrawable(Resources.java:1915)
E/AndroidRuntime(1461):     ... 33 more

该错误的推移而上。基本上指的是对XML。

The error goes on and on. Basically refering to the XML.

将其返回到 @色/红色工作正常。

我该如何解决这个问题?

How can I fix this?

推荐答案

安卓不允许在我的经验引用一个颜色在绘制的属性。我解决了这个问题,一种解决方法创建每个主题一个形状。 我张贴的说明作为一个答案,一个previous的问题在这里:

Android doesn't allow in my experience to reference a color with an attribute in a drawable. I resolved the problem with a workaround creating one shape per theme. I posted a description as an answer to a previous question here:

http://stackoverflow.com/a/13471695/891479

这篇关于创建自定义样式崩溃的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆