我刚刚在一个SDK 16设备和模拟器上测试了我的应用程序,两次都出现了以下错误:
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
中:<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">?attr/colorPrimary</item>
</style>
即使使用SVG可绘制对象,它也可以工作:
<style name="BulletListItemIcon.Drink">
<item name="srcCompat">@drawable/ic_profile</item>
</style>
在我的应用程序的gradle构建中,我设置了
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
}
}
在上述ImageView风格中使用颜色或SVG可绘制图像可以正常工作,但是使用自定义绘制图像则失败了。我不知道为什么会出现这种情况,所以我非常感谢任何提示和答案,谢谢 :)