【问题标题】:Error inflating class FloatingActionButton膨胀类 FloatingActionButton 时出错
【发布时间】:2015-01-09 12:52:32
【问题描述】:

我用https://github.com/makovkastar/FloatingActionButton

布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:fab="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            />

    <!-- The main content view -->
    <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent">
            <com.melnykov.fab.ObservableScrollView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/obsScrollView">
                <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:background="#edeff2">
                    ...
                </LinearLayout>
            </com.melnykov.fab.ObservableScrollView>
            <com.melnykov.fab.FloatingActionButton
                    android:id="@+id/fab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|right"
                    android:layout_margin="16dp"
                    android:src="@drawable/ic_favorite_grey600_48dp"
                    fab:fab_colorNormal="@color/primary"
                    fab:fab_colorPressed="@color/primary_pressed"
                    fab:fab_colorRipple="@color/ripple"/>
        </FrameLayout>
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

代码:

...
// ScrollView
scrollView = (ObservableScrollView) findViewById(R.id.obsScrollView);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.attachToScrollView(scrollView);
fab.setColorNormal(getResources().getColor(R.color.primary));
fab.setColorPressed(getResources().getColor(R.color.primary_pressed));
...

但是当我参加这个活动时出现错误:

01-08 19:44:01.371    2311-2311/com.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.myapp, PID: 2311
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.VideoActivity}: android.view.InflateException: Binary XML file line #23: Error inflating class com.melnykov.fab.ObservableScrollView
...
Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.melnykov.fab.ObservableScrollView

请告诉我,我做错了什么

应用程序编译没有错误

依赖关系:

当我在 XML 中设置 attr 时:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:fab="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
...
<com.melnykov.fab.FloatingActionButton
                    android:id="@+id/fab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|right"
                    android:layout_margin="16dp"
                    android:src="@drawable/ic_favorite_grey600_48dp"
                    fab:fab_colorNormal="@color/primary"
                    fab:fab_colorPressed="@color/primary_pressed"
                    fab:fab_colorRipple="@color/ripple"/>
...

错误:

Error:(137, -1) android-apt-compiler: [myapp] /Users/fedor/IdeaProjects/myapp/res/layout/video.xml:137: error: No resource identifier found for attribute 'fab_colorNormal' in package 'com.example.myapp'
Error:(137, -1) android-apt-compiler: [myapp] /Users/fedor/IdeaProjects/myapp/res/layout/video.xml:137: error: No resource identifier found for attribute 'fab_colorPressed' in package 'com.example.myapp'
Error:(137, -1) android-apt-compiler: [myapp] /Users/fedor/IdeaProjects/myapp/res/layout/video.xml:137: error: No resource identifier found for attribute 'fab_colorRipple' in package 'com.example.myapp'

但是我导入了库

ObservableScrollView:

package com.melnykov.fab;


import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class ObservableScrollView extends ScrollView {

    public interface OnScrollChangedListener {
        void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt);
    }

    private OnScrollChangedListener mOnScrollChangedListener;

    public ObservableScrollView(Context context) {
        super(context);
    }

    public ObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollChangedListener != null) {
            mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }

    public void setOnScrollChangedListener(OnScrollChangedListener listener) {
        mOnScrollChangedListener = listener;
    }
}

【问题讨论】:

标签: android android-appcompat floating-action-button


【解决方案1】:

我开始使用 Gradle,它解决了所有问题

【讨论】:

    【解决方案2】:

    在您的 XML 文件中添加 xmlns:fab="http://schemas.android.com/tools"。这将修复“找不到属性的资源标识符...”错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 2017-02-28
      • 2021-07-10
      • 2020-08-01
      • 2016-02-28
      • 2013-05-17
      • 2016-09-22
      相关资源
      最近更新 更多