我正在尝试向我的应用添加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)
我用Google搜索了一些内容,它告诉我必须在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
似乎存在问题,因为当我删除该文件时,设计器不会显示任何错误。但是,如何解决所有这些问题……?
答案 0 :(得分:0)
您需要导入
com.google.android.material.snackbar.Snackbar
代替
android.support.design.widget.Snackbar
答案 1 :(得分:0)
您应该使用的是这个
implementation "com.google.android.material:material:1.1.0-alpha06"
如果您知道如何以支持方式配置依赖项,则可以查看Migrating to AndroidX
上的Android开发者页面。
另外,请检查最新版本,在您阅读本文时,它可能不是1.1.0-alpha06
。
答案 2 :(得分:-1)
我添加到了build.gradle(Module:app)
implementation 'com.android.support:design:28.0.0'
它工作正常,并且我的gradle成功建立。
这些是我的依赖项:-
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android:flexbox:1.1.0'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.0.0'
implementation 'com.android.support:design:28.0.0'
}