Android:在使用自定义drawable时,在预先Lollipop(SDK 16)上膨胀类ImageView时出错?
我只是testet我的应用程序一个SDK 16设备上,并在模拟器上和两次我得到以下错误:Android:在使用自定义drawable时,在预先Lollipop(SDK 16)上膨胀类ImageView时出错?
E/AndroidRuntime: FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #9: Error inflating class ImageView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.eliburgi.foodallergydiary.ui.diarypage.adapters.DrinkEntryBulletAdapter.onCreateViewHolder(DrinkEntryBulletAdapter.java:53)
at com.eliburgi.foodallergydiary.ui.diarypage.adapters.DrinkEntryBulletAdapter.onCreateViewHolder(DrinkEntryBulletAdapter.java:23)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6367)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5555)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3025)
at android.view.View.measure(View.java:15172)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at android.view.View.measure(View.java:15172)
at android.support.v7.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:8636)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1577)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1795)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1391)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:870)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.jav
在SDK 21及以上的一切工作正常,但对于早期版本程序无法解析如下ImageView的:
<ImageView
style="@style/BulletListItemIcon.Drink"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/margin_left_diary_entry_bullet_keyline"
android:layout_marginStart="@dimen/margin_left_diary_entry_bullet_keyline" />
在styles.xml:
<style name="BulletListItemIcon">
<item name="android:layout_width">@dimen/icon_small</item>
<item name="android:layout_height">@dimen/icon_small</item>
</style>
<style name="BulletListItemIcon.Drink">
<item name="srcCompat">?attr/iconBackgroundDrink</item>
</style>
?attr/iconBackgroundDrink
在AppTheme定义用于此应用:
<item name="iconBackgroundDrink">@drawable/bg_drink_icon_gradient</item>
在bg_drink_icon_gradient.xml
:
<style name="BulletListItemIcon.Drink">
<item name="srcCompat">?attr/colorPrimary</item>
</style>
它,即使使用SVG绘制的作品:
<shape android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient xmlns:android="http://schemas.android.com/apk/res/android"
android:angle="135"
android:startColor="@color/drink_gradient"
android:endColor="@color/drink_gradient_dark"
android:type="linear" />
<stroke android:color="@color/icon_border" android:width="@dimen/border_width_icons"/>
</shape>
不知怎的,使用一种颜色,而不是被拉伸时,一切工作正常
<style name="BulletListItemIcon.Drink">
<item name="srcCompat">@drawable/ic_profile</item>
</style>
在我的gradle app's构建我设置vectorDrawables.useSupportLibrary = true
:
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.eliburgi.foodallergydiary"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
使用上述风格的颜色或SVG绘制的ImageView的工作正常,但它无法使用自定义绘制。我不知道为什么这不起作用,所以我会很感激任何提示和答案,谢谢:)
似乎是我的自定义XML可绘制的一些错误。我评论了中风标签,它工作正常。我会尝试一些有什么缺失,并让你知道。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/blue"/>
<!--<stroke android:color="@color/icon_border" android:width="@dimen/border_width_icons"/>-->
</shape>
如果有人知道什么是错的,请让我知道。
编辑:我想我知道是什么导致错误。 It's所有自定义XML可绘中风标签使用下列颜色定义that's:
<color name="icon_border">?android:colorBackground</color>
任何人都可以请向我解释为什么不上SDK 16工作吗?
编辑:它现在的作品。我简单地更换上面的颜色定义有:
<color name="app_background">##FFFAFAFA</color>
<color name="icon_border">@color/app_background</color>
从来就设定主题属性android:backgroundColor
我的色彩app_background
。
感谢您的帮助:)
事实上,你不能引用主题属性API 21之前,使用程序兼容性实现的时候唯一的例外是矢量绘图资源。 – BladeCoder
我一直在遇到同样的问题。解决的办法是用android.support.v7.widget.AppCompatImageView替换ImageView
您是否尝试过使用除了srcCompat之外的src? – jonathanrz
是的。同样的错误... – Elias
和背景?或backgroundDrawable? – jonathanrz