gpt4 book ai didi

android - 应用程序 :srcCompat in xml 的 InflateException

转载 作者:行者123 更新时间:2023-11-30 00:17:14 25 4
gpt4 key购买 nike

在我的 .xml 布局中使用带有矢量可绘制对象的 app:srcCompat 时,我无法在运行 android 4.2.1 的设备上运行代码。例如我有一个像这样的 FAB:

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
...
android:tint="@color/colorWhite"
app:backgroundTint="@color/colorPrimary"
app:srcCompat="@drawable/ic_add"/>

其中 ic_add 是矢量可绘制对象:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>

这在 Lollipop 上运行良好,但在运行 android 4.2.1 的设备上崩溃:

FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #14: Error inflating class android.support.design.widget.FloatingActionButton
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
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 info.samuilife.samuiplus.user.offers.OffersFragment.onCreateView(OffersFragment.kt:34)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2239)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2034)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1239)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1087)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1613)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:713)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1389)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:783)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2191)
at android.view.View.measure(View.java:15604)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)

没有 app:srcCompat 行它工作正常。

在我的 build.gradle 中我还有:

defaultConfig {
vectorDrawables.useSupportLibrary = true
...
}

...

dependencies {
...
compile "com.android.support:appcompat-v7:26.0.0-alpha1"
}

我在这里错过了什么?任何想法/想法将不胜感激!

解决方案: This答案对我有用。

最佳答案

您可能忘记添加设计支持库。因此,您应该通过添加以下行在您的依赖项中添加设计支持库:

dependencies {
...
compile "com.android.support:design:27.0.0"
//compile "com.android.support:appcompat-v7:26.0.0-alpha1"
}

您不需要添加 appcompat 库,因为它隐式包含在设计支持库中。请记住永远不要在您的代码中使用库的 alpha 版本。

阅读更多关于设计支持库的信息 Using the Design Support Library


更新

添加Vector Drawable Support与:

compile "com.android.support:support-vector-drawable:27.0.0"

关于android - 应用程序 :srcCompat in xml 的 InflateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47018815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com