【发布时间】:2019-03-11 02:56:28
【问题描述】:
我正在尝试将Snackbar 添加到我的应用中,
Snackbar.make(findViewById(android.R.id.content),
R.string.not_found, Snackbar.LENGTH_INDEFINITE).show();
但遇到此错误消息:
10-05 16:48:24.530 28492-28492/nl.minerall.sapphire.pocket.full E/AndroidRuntime: FATAL EXCEPTION: main
Process: nl.minerall.sapphire.pocket.full, PID: 28492
android.view.InflateException: Binary XML file line #41: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.google.android.material.snackbar.Snackbar.make(Snackbar.java:188)
at com.google.android.material.snackbar.Snackbar.make(Snackbar.java:230)
我用谷歌搜索了一些,它告诉我必须将com.android.support:design 添加到我的build.gradle,但这完全破坏了我的构建,因为我使用的是 AndroidX:
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.android:flexbox:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.legacy:legacy-support-v13:1.0.0'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}
更新我翻了一下,发现这个 XML 文件正在膨胀 (/extras/android/support/design/res/layout/design_layout_snackbar_include.xml):
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2015 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/snackbar_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="@dimen/design_snackbar_padding_vertical"
android:paddingBottom="@dimen/design_snackbar_padding_vertical"
android:paddingLeft="@dimen/design_snackbar_padding_horizontal"
android:paddingRight="@dimen/design_snackbar_padding_horizontal"
android:textAppearance="@style/TextAppearance.Design.Snackbar.Message"
android:maxLines="@integer/design_snackbar_text_max_lines"
android:layout_gravity="center_vertical|left|start"
android:ellipsize="end"
android:textAlignment="viewStart"/>
<Button
android:id="@+id/snackbar_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/design_snackbar_extra_spacing_horizontal"
android:layout_marginStart="@dimen/design_snackbar_extra_spacing_horizontal"
android:layout_gravity="center_vertical|right|end"
android:paddingTop="@dimen/design_snackbar_padding_vertical"
android:paddingBottom="@dimen/design_snackbar_padding_vertical"
android:paddingLeft="@dimen/design_snackbar_padding_horizontal"
android:paddingRight="@dimen/design_snackbar_padding_horizontal"
android:visibility="gone"
android:textColor="?attr/colorAccent"
style="?attr/borderlessButtonStyle"/>
</merge>
当只是将该文件复制到我的项目并在 Android Studio 中打开它时,?attr/colorAccent 似乎有问题,因为当我删除它时,设计器没有显示任何错误。但是,如何解决这一切...?
【问题讨论】:
-
到目前为止我还没有使用过snacker,但我最近开始使用androidx,我通过从sunflower示例应用程序复制解决了我所有的依赖问题。因为在线经常导入支持库,您正确地说它不会像androidx那样工作。所以看一下,这个片段使用了snackbar。 gradle 导入通常在 build.gradle 中:github.com/googlesamples/android-sunflower/blob/master/app/src/…
-
实际读取你的异常,你确定是导入的问题吗?你可以发布你的xml文件吗?因为异常确实正确引用了 android 材质的snackbar。我认为问题出在 xml 文件的第 41 行。
-
@findusl 没有 XML。我只想能够在任何布局上使用
Snackbar而不是Toast。我尝试使用android.R.id.content作为通用 ID。 -
@findusl 我来看看那个向日葵代码。
-
不知道对你有没有帮助,不过这个好像是xml膨胀失败:github.com/material-components/material-components-android/blob/…