RuntimeException: Could not inflate Behavior subcl

2020-06-08 13:35发布

I new in android and I've troubles with FloatingActionButton behaivors

My custom behavoir 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:

...

<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:

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?

5条回答
乱世女痞
2楼-- · 2020-06-08 14:13

Add the following two constructors to your FooterBehavior:

public FooterBehavior() {
}

public FooterBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
}
查看更多
Evening l夕情丶
3楼-- · 2020-06-08 14:19

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"
查看更多
看我几分像从前
4楼-- · 2020-06-08 14:25

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.

Find your element here and replace:

Support:

android.support.design.widget.FloatingActionButton

AndroidX:

com.google.android.material.floatingactionbutton.FloatingActionButton
查看更多
放我归山
5楼-- · 2020-06-08 14:27

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:

<android.support.design.widget.FloatingActionButton
        ...
        app:layout_behavior="com.example.xyzreader.ScrollingFABBehavior"/>
查看更多
再贱就再见
6楼-- · 2020-06-08 14:31

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

查看更多
登录 后发表回答