我想在我的应用程序上使用FloatingActionButton,我读到这个:https://guides.codepath.com/android/Floating-Action-Buttons#google-s-official-support-library但是当我运行Activity时我有这个错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xavier_laffargue.podcast/com.xavier_laffargue.podcast.ACT_Test}: android.view.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.CoordinatorLayout
XML文件
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lvToDoList"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="24dp"
android:src="@drawable/ic_action_refresh"
app:layout_anchor="@id/lvToDoList"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
Graddle
*apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.xavier_laffargue.podcast"
minSdkVersion 21
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:support-v4:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:support-v13:22.2.0'
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
}
如果您使用Activity,请将其更改为AppCompatActivity。它可能是活动时的错误。
您需要在应用程序的构建中添加以下支持库。
compile 'com.android.support:design:23.0.1'
那是去年,现在的最新版本是
compile 'com.android.support:design:27.0.2'
对我来说,当使用androidx
和android.support.v7
库的混合时,我遇到了这个错误。
请在此处查看我的解决方案,了解此错误的版本:https://stackoverflow.com/a/52490825/1762493
适用于使用AndroidX Dependency的用户。在你的xml文件中确保所有你的
android.support.???
CoordinatorLayout中的节点(如包含的Action Bars)也替换为
androidx.??? (or com.google.android.???)
那些 。
它对我有用
禁用即时运行
File => Settings => Build,Execution,Deployment => Instant Run
只需在FloatingActionButton(app
而不是android
)中使用此行:
app:backgroundTint="@color/colorAccent"
请将compile 'com.android.support:design:23.0.1'
放在项目build.gradle文件中
我有同样的错误。只需将项目父主题更改为
<style name="Base.AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
或任何其他Theme.AppCompat *
适用于使用AndroidX Dependency的用户
除了更改依赖项外,还必须更改XML。
从
<android.support.design.widget.CoordinatorLayout
至
<androidx.coordinatorlayout.widget.CoordinatorLayout
需要两个:
public class MyActivity extends AppCompatActivity
<style name="MyStyle" parent="Theme.AppCompat">
另外:
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
(支持Android API 7到22+)
Android.Support.V7.App.AppCompatActivity
Theme.AppCompat.*
风格。所以你的Resources\values\styles.xml
应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="YourAppTheme" parent="Theme.AppCompat">
<item name="colorPrimaryDark">#AB000D</item>
<item name="colorPrimary">#E53935</item>
<item name="colorAccent">#00B8D4</item>
</style>
<!-- other styles... -->
</resources>
AndroidManifest.xml
中的主题
<application android:theme="@style/YourAppTheme"></application>
将这些依赖项添加到gradle文件中。在某些情况下,这可以解决问题。
dependencies {
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
...
}