簡體   English   中英

如何在Android中刪除浮動操作按鈕膨脹異常?

[英]how to remove floating action button inflating exception in android?

我在應用程序中使用FAB(浮動操作按鈕),但是這使過去幾天的生活變得很艱難。 每當我創建具有單個活動的新項目時FAB都能正常工作,但是每當我添加新活動時,它就會開始給我例外

android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.floating action button.

我的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>

我也正在使用Appcompat主題,並嘗試了一切,但問題仍然相同。

以下是我的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>

以下是我在課堂上為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();
        }
    });

將以下代碼用於XML中的“浮動操作”按鈕
並進行相關更改

<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" />

如果仍然會給出相同的錯誤,請提供Activity文件和gradle文件

請參考這個 可能是您一定錯過了什么

您是否已在build.gradle中添加了compile'c​​om.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM