java 运行时异常:无法膨胀行为子类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45100963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:30:17  来源:igfitidea点击:

RuntimeException: Could not inflate Behavior subclass

javaandroidfloating-action-buttoncoordinator-layout

提问by Aleksey Leonov

I new in android and I've troubles with FloatingActionButtonbehaivors

我是 android 新手,但我在FloatingActionButton行为方面遇到了麻烦

My custom behavtheitroad class:

我的自定义行为类:

public class ScrollingFABBehavior extends FloatingActionButton.Behavior {
    private static final String TAG = "ScrollingFABBehavior";

    public ScrollingFABBehavior(Context context, AttributeSet attrs,
            Handler mHandler) {
        super();
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
            FloatingActionButton child, View directTargetChild, View target,
            int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
                || super.onStartNestedScroll(coordinatorLayout, child,
                        directTargetChild, target, nestedScrollAxes);
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout,
            FloatingActionButton child, View target, int dxConsumed,
            int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed,
                dyConsumed, dxUnconsumed, dyUnconsumed);
        if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
            child.hide();
        } else if (dyConsumed < 0 && child.getVisibility() == View.GONE) {
            child.show();
        }
    }

    @Override
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout,
            FloatingActionButton
            child, View target) {
        super.onStopNestedScroll(coordinatorLayout, child, target);
    }
}

Fragment XML:

片段 XML:

...

...

<android.support.design.widget.FloatingActionButton
        android:id="@+id/share_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:contentDescription="@string/action_share"
        android:elevation="@dimen/fab_elevation"
        android:src="@drawable/ic_share"
        app:layout_behavior=".ScrollingFABBehavior"/>

</android.support.design.widget.CoordinatorLayout>

RuntimeError when fragment inflate xml:

片段膨胀 xml 时的运行时错误:

07-14 08:52:43.904 30785-30785/com.example.xyzreader E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.example.xyzreader, PID: 30785
                                                                       android.view.InflateException: Binary XML file line #115: Could not inflate Behavior subclass com.example.xyzreader.ui.ScrollingFABBehavior
                                                                       Caused by: java.lang.RuntimeException: Could not inflate Behavior subclass com.example.xyzreader.ui.ScrollingFABBehavior
                                                                           at android.support.design.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:615)
                                                                           at android.support.design.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2652)

e.t.c

等等

Whats wrong?

怎么了?

回答by PangoSea

Add the following two constructors to your FooterBehavior:

将以下两个构造函数添加到您的FooterBehavior:

public FooterBehavior() {
}

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

回答by Md Imran Choudhury

If you are using AndroidX (open-source project that the Android team uses to develop, test, package, version and release libraries within Jetpack) then you need to update XML.

如果您使用的是 AndroidX(Android 团队用于在 Jetpack 中开发、测试、打包、版本和发布库的开源项目),那么您需要更新 XML。

Find your element hereand replace:

在此处找到您的元素并替换:

Support:

支持:

android.support.design.widget.FloatingActionButton

AndroidX:

安卓X:

com.google.android.material.floatingactionbutton.FloatingActionButton

回答by Mahmoud

Make sure that you are using the correct path of the custom behavior class.

确保您使用的是自定义行为类的正确路径。

For example:

例如:

app:layout_behavior="net.company.myapp.view.behavior.TextViewBehavior"

回答by marius bardan

In case your app has a different package ID than your actual package structure, make sure you specify the full path to your custom behavior class:

如果您的应用程序的包 ID 与实际包结构不同,请确保指定自定义行为类的完整路径:

<android.support.design.widget.FloatingActionButton
        ...
        app:layout_behavior="com.example.xyzreader.ScrollingFABBehavior"/>

回答by Aleksey Leonov

Solved. Change app:layout_behavior=".ScrollingFABBehavior"/>to app:layout_behavior=".ui.ScrollingFABBehavior"/>

解决了。更改app:layout_behavior=".ScrollingFABBehavior"/>app:layout_behavior=".ui.ScrollingFABBehavior"/>