我正在开发一个带有活动的简单应用程序,我使用碎片.一些屏幕上有一些元素.当我编译并运行应用程序时它工作正常,除了三星Galaxy s3和注释4.我没有从stacktrace得到它有什么不对.
01-16 16:25:05.915: E/AndroidRuntime(23174): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.MainActivity}: android.view.InflateException: Binary XML file line #37: Error inflating class com.android.internal.widget.ActionBarView
01-16 16:25:05.915: E/AndroidRuntime(23174): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
01-16 16:25:05.915: E/AndroidRuntime(23174): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
01-16 16:25:05.915: E/AndroidRuntime(23174): at android.app.ActivityThread.access$800(ActivityThread.java:163)
01-16 16:25:05.915: E/AndroidRuntime(23174): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
01-16 16:25:05.915: E/AndroidRuntime(23174): at android.os.Handler.dispatchMessage(Handler.java:102)
01-16 16:25:05.915: E/AndroidRuntime(23174): at android.os.Looper.loop(Looper.java:157)
01-16 16:25:05.915: E/AndroidRuntime(23174): at android.app.ActivityThread.main(ActivityThread.java:5335)
01-16 16:25:05.915: E/AndroidRuntime(23174): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 16:25:05.915: E/AndroidRuntime(23174): at java.lang.reflect.Method.invoke(Method.java:515)
01-16 16:25:05.915: E/AndroidRuntime(23174): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
01-16 16:25:05.915: E/AndroidRuntime(23174): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
01-16 16:25:05.915: E/AndroidRuntime(23174): …
Run Code Online (Sandbox Code Playgroud) 在给我的布局充气时,我得到了这个例外:
E AndroidRuntime: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class <unknown>
E AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
E AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
E AndroidRuntime: at com.myapp.view.MyRecyclerAdapter.onCreateViewHolder(MyRecyclerAdapter:80)
E AndroidRuntime: at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5288)
E AndroidRuntime: at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4551)
E AndroidRuntime: at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
E AndroidRuntime: at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
Run Code Online (Sandbox Code Playgroud)
日志中没有"由...引起",但我添加了代码来捕获异常并调用,getCause()
直到它返回null,这是事件序列:
android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class <unknown>
android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
Error: Binary XML file line #11: …
Run Code Online (Sandbox Code Playgroud) 带有标签的我的应用有两个主题.在每个主题中,选项卡具有处于选定和未选定状态的不同图像.我如何按主题正确引用图像?
例如.我有themes.xml
<?xml version="1.0" encoding="utf-8"?>
<style name="LightTheme" parent="@android:style/Theme.Light">
<item name="tabShows">@drawable/ic_tab_shows_unselected_light</item>
<item name="tabShowsSelected">@drawable/ic_tab_shows_selected_light</item>
<item name="tabNews">@drawable/ic_tab_news_selected_light</item>
<item name="tabNewsSelected">@drawable/ic_tab_news_unselected_light</item>
</style>
<style name="DarkTheme" parent="@android:style/Theme.Black">
<item name="tabShows">@drawable/ic_tab_shows_unselected_dark</item>
<item name="tabShowsSelected">@drawable/ic_tab_shows_selected_dark</item>
<item name="tabNews">@drawable/ic_tab_news_selected_dark</item>
<item name="tabNewsSelected">@drawable/ic_tab_news_unselected_dark</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我还有一个tab_shows.xml和tab_news.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/ic_tab_shows_selected_light"/>
<item android:state_selected="false" android:drawable="@drawable/ic_tab_shows_unselected_light" />
Run Code Online (Sandbox Code Playgroud)
如何根据当前主题在选择器中引用所需的图像?这不适合我
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="?tabShowsSelected"/>
<item android:state_selected="false" android:drawable="?tabShows" />
Run Code Online (Sandbox Code Playgroud)
在布局文件中这是有效的,我的意思是通过?styleName引用样式
假设我有一个简单的形状drawable绘制环如下:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="36"
android:useLevel="false" >
<gradient
android:centerColor="@android:color/holo_blue_bright"
android:centerY="0.001"
android:endColor="@android:color/holo_blue_dark"
android:gradientRadius="202.5"
android:startColor="@android:color/transparent"
android:type="radial"
android:useLevel="false" />
</shape>
Run Code Online (Sandbox Code Playgroud)
我将此形状应用为视图背景,如下所示:
<View
android:layout_width="96dp"
android:layout_height="96dp"
android:padding="16dp"
android:background="@drawable/custom_shape" />
Run Code Online (Sandbox Code Playgroud)
形状的确切细节与这个问题无关 - 只要说我有一个形状就足够了.现在,我不想硬编码形状的各种参数.我们thicknessRatio
以此为例.如果我想根据屏幕配置改变厚度比,我当然会使用整数资源,如下所示.我有一个带有以下内容的values.xml:
<item name="thickness_ratio" type="integer" format="integer">56</item>
然后,android:thicknessRatio="@integer/thickness_ratio"
.
到现在为止还挺好.现在,我也希望我的形状有两种"风格" - 一个"大"和一个"小",我希望查询厚度比不依赖于配置,而是根据应用于视图的样式.这是什么样式和主题.所以,这是我尝试过的:
<resources>
<attr name="thicknessRatio" format="reference" />
</resources>
Run Code Online (Sandbox Code Playgroud)
<style name="CustomShapeSmall">
<item name="thicknessRatio">@integer/thickness_ratio_small</item>
</style>
<style name="CustomShapeLarge">
<item name="thicknessRatio">@integer/thickness_ratio_large</item>
</style>
Run Code Online (Sandbox Code Playgroud)
<item name="thickness_ratio_small" type="integer" format="integer">32</item>
<item name="thickness_ratio_large" type="integer" format="integer">56</item>
Run Code Online (Sandbox Code Playgroud)
android:thicknessRatio="?attr/thicknessRatio"
View
:<View
android:layout_width="96dp"
android:layout_height="96dp"
android:padding="16dp" …
Run Code Online (Sandbox Code Playgroud) 我正在使用attr为我的项目创建一个可绘制的选择器,这样一旦我改变主题颜色,我就不必对可绘制文件进行任何更改.我正在使用以下libs:
compile 'com.android.support:appcompat-v7:+'
compile 'com.android.support:cardview-v7:+'
compile 'com.android.support:design:22.2.0'
Run Code Online (Sandbox Code Playgroud)
这是drawable的源代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="?attr/colorPrimary" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="?attr/colorPrimaryDark" android:state_pressed="true"/>
<item android:drawable="@android:color/darker_gray" android:state_enabled="false"/>
<item android:drawable="?attr/colorPrimary"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
在这个相同的代码中,如果我用colors.xml文件中定义的颜色替换属性,同样的drawable工作.
样本可绘制颜色:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/color_primary" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="@color/color_primary_dark" android:state_pressed="true"/>
<item android:drawable="@android:color/darker_gray" android:state_enabled="false"/>
<item android:drawable="@color/color_primary"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
提前致谢!
这是来自Android Market的崩溃日志:
android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:596)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:644)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
at com.designfuture.music.ui.dialog.MXMDialog.<init>(MXMDialog.java:73)
at com.designfuture.music.ui.dialog.MXMDialog.<init>(MXMDialog.java:58)
at com.designfuture.music.model.MXMStoreBuy.getBuyDialog(MXMStoreBuy.java:70)
at com.designfuture.music.ui.fragment.plbl.XLBLFragment$ViewHolder$3.onClick(XLBLFragment.java:290)
at android.view.View.performClick(View.java:3110)
at android.view.View$PerformClick.run(View.java:11934)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:416)
at android.view.LayoutInflater.createView(LayoutInflater.java:576)
... 22 more
Caused by: java.lang.UnsupportedOperationException: Can't convert to color: …
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我定义了我在自定义主题中设置的颜色属性:
RES /值/ attrs.xml
<resources>
<attr name="bbColorPrimary" format="color|reference" />
</resources>
Run Code Online (Sandbox Code Playgroud)
RES /值/ colors.xml
<resources>
<color name="white">#ffffff</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
RES /值/ style.xml
<style name="MyStyle" parent="@style/Theme.AppCompat.NoActionBar">
<item name="bbColorPrimary">@color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)
RES /抽拉/ background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
<shape>
<solid android:color="?attr/bbColorPrimary" />
</shape>
</item>
<item>
<shape>
<solid android:color="@color/transparent" />
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
res/drawable/background.xml
被设置为某个按钮的背景.应用程序在膨胀res/drawable/background.xml
文件时崩溃,但有以下异常:
...
Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
at android.content.res.TypedArray.getColor(TypedArray.java:326)
at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:748)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:172)
....
Run Code Online (Sandbox Code Playgroud)
它适用于高于10的API级别.如果我删除?attr/bbColorPrimary
它工作正常,尽管?attr/..
在其他资源文件中设置了许多其他调用.我没有使用任何特定于版本的资源文件夹.
我在这做错了什么?
我正在构建我的应用程序,以便它可以受益于多个主题.
我已经到了我想添加自定义项目的阶段,例如
<style name="My.Theme.Default">
...
<item name="borderColorDialog">@color/red</item>
...
</style>
<style name="My.Theme.Blue">
...
<item name="borderColorDialog">@color/blue</item>
...
</style>
Run Code Online (Sandbox Code Playgroud)
所以我已经将属性添加borderColorDialog
到我的attr文件中,如下所示:
<attr name="borderColorDialog" format="color" />
Run Code Online (Sandbox Code Playgroud)
现在我想测试它.我有一个drawable如下:
<?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>
Run Code Online (Sandbox Code Playgroud)
这对我来说在逻辑上看起来都很合理,但?borderColorDialog
引用会使模拟器崩溃:
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) …
Run Code Online (Sandbox Code Playgroud) 我正在开始一个新项目,所以项目或多或少"空".我刚刚添加了MainActivity,带有MainActivtyFragment.我没有向他们添加任何代码.
现在,我编辑styles.xml,看起来像这样:
<resources>
<!-- Base application theme. -->
<style name="MyTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="android:windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
<!-- colorPrimary is used for the default action bar background -->
<item name="android:colorPrimary">#2196F3</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="android:colorPrimaryDark">#1976D2</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="android:colorAccent">#FF4081</item>
<!-- You can also …
Run Code Online (Sandbox Code Playgroud)我的主题定义为
<style name="AppThemeRed" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimaryRed</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkRed</item>
<item name="colorAccent">@color/colorAccentRed</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在我的 XML 布局中,我正在做
<android.support.design.widget.AppBarLayout
android:background="?attr/colorPrimary"
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
Run Code Online (Sandbox Code Playgroud)
每当我更改任何主题时,colorPrimary 都会发生变化
但是,如果我在 drawable 中添加了相同的东西,例如
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
<corners android:radius="5dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
<corners android:radius="5dp"/>
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
它因无法膨胀视图而崩溃,该视图的背景设置为 @drawabe/xxxx
如何在我的 XML drawable 中定义主题颜色属性
我在 RecyclerView 中添加了一个 FastScroller 并使用了一个“气泡”-Drawable。
如果我在 API 21 上测试我的应用程序,它可以工作,但如果我在 API 19 上测试它,它会崩溃:
java.lang.RuntimeException: 无法启动活动 ComponentInfo{at.guger.musixs/at.guger.musixs.ui.MainActivity}:
android.view.InflateException:二进制 XML 文件第 15 行:错误膨胀类 at.guger.fastscroll.FastScroller ... 引起:android.view.InflateException:二进制 XML 文件第 15 行:错误膨胀类 at.guger.fastscroll .FastScroller
...引起:java.lang.reflect.InvocationTargetException
... 引起:android.view.InflateException:二进制 XML 文件第 6 行:错误膨胀类 at.guger.fastscroll.FastScrollBubble
...引起:java.lang.reflect.InvocationTargetException
... 引起:android.content.res.Resources$NotFoundException: File res/drawable/bubble.xml from drawable resource ID #0x7f02004b
在 android.content.res.Resources.loadDrawable(Resources.java:3457)
在 android.content.res.TypedArray.getDrawable(TypedArray.java:602)
在 android.view.View.(View.java:3767)
在 android.view.ViewGroup.(ViewGroup.java:481)
在 android.widget.FrameLayout.(FrameLayout.java:101)
在 android.widget.FrameLayout.(FrameLayout.java:97)
在 at.guger.fastscroll.FastScrollBubble.(FastScrollBubble.java:0)
... 38
我的气泡 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:topLeftRadius="@dimen/bubble_corner_radius"
android:topRightRadius="@dimen/bubble_corner_radius"
android:bottomLeftRadius="@dimen/bubble_corner_radius"
android:bottomRightRadius="0dp" />
<solid android:color="?attr/colorAccent" …
Run Code Online (Sandbox Code Playgroud)