[英]how to remove floating action button inflating exception in android?
I am using FAB(floating action button) in the app ,but it is making my life difficult from the past few days. 我在应用程序中使用FAB(浮动操作按钮),但是这使过去几天的生活变得很艰难。 Whenever I create new Project with single activity FAB work properly, but whenever I add new activities , it starts giving me exception 每当我创建具有单个活动的新项目时FAB都能正常工作,但是每当我添加新活动时,它就会开始给我例外
android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.floating action button.
My xml code is given below: 我的xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
I am using Appcompat theme as well and tried everything but problem remain the same. 我也正在使用Appcompat主题,并尝试了一切,但问题仍然相同。
Following is my Xml file for FAB 以下是我的FAB的Xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.vote3d.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.Toolbar
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorAccent"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:navigationIcon="@mipmap/ic_menu_36dp">
<ImageView
android:id="@+id/iv_toolbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginRight="50dp"
android:src="@drawable/logo"/>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
app:tabGravity="fill"
android:background="#e00000"
app:tabIndicatorColor="@android:color/white"
app:tabMode="fixed"
app:tabSelectedTextColor="@android:color/white" />
</android.support.design.widget.AppBarLayout>
<!-- <include layout="@layout/content_main" />-->
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="#ffffff"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin" />
</android.support.design.widget.CoordinatorLayout>
Following is the code I am using for FAB in class 以下是我在课堂上为FAB使用的代码
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Use Below code For Floation Action Button in your XML 将以下代码用于XML中的“浮动操作”按钮
and do related changes 并进行相关更改
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
if still it will gives the same error please provide Activity file and gradle file 如果仍然会给出相同的错误,请提供Activity文件和gradle文件
您是否已在build.gradle中添加了compile'com.android.support:design:25.1.0 compile 'com.android.support:design:25.1.0'
吗?
在您的style.xml文件中添加“ Theme.AppCompat.Light.DarkActionBar”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.