【问题标题】:RuntimeException: Could not inflate Behavior subclassRuntimeException:无法膨胀行为子类
【发布时间】:2017-12-19 10:28:39
【问题描述】:

我是 android 新手,我遇到了 FloatingActionButton 行为问题

我的自定义行为类:

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);
    }
}

片段 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>

片段膨胀 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

怎么了?

【问题讨论】:

  • 错误的构造函数 ....
  • 更改为:public Sc​​rollingFABBehavior() { super(); } 。没有任何改变

标签: java android floating-action-button coordinator-layout


【解决方案1】:

解决了。将app:layout_behavior=".ScrollingFABBehavior"/&gt; 更改为app:layout_behavior=".ui.ScrollingFABBehavior"/&gt;

【讨论】:

  • 这对任何人有什么帮助?谁知道你的包裹签名,你犯了什么错误还不清楚。
  • 其实他是对的,对我来说,所有的包名都有效,而不仅仅是类名。
【解决方案2】:

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

public FooterBehavior() {
}

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

【讨论】:

  • 对我没有帮助
  • Kotlin 版本:class ScrollingFABBehavior(context: Context?, attrs: AttributeSet?) : FloatingActionButton.Behavior(context, attrs) { constructor() : this(null, null) }
  • 可能有一个简化的 Kotlin 版本:class ScrollingFABBehavior @JvmOverloads constructor(context: Context? = null, attrs: AttributeSet? = null) {...}
【解决方案3】:

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

找到你的元素 here 并替换:

支持:

android.support.design.widget.FloatingActionButton

AndroidX:

com.google.android.material.floatingactionbutton.FloatingActionButton

【讨论】:

    【解决方案4】:

    确保您使用的是绝对路径,而不是自定义行为类的相对路径。

    例如:

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

    【讨论】:

    • 在我的情况下,Android Studio 将我的绝对路径更改为相对路径。我的包名与 id 不同,所以它破坏了它。
    • 尝试为行为子类使用绝对路径而不是相对路径,因为在运行时Android可能无法解析相对路径
    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:

      如果您在 Android X 中使用 BottomSheet,则必须使用

      app:layout_behavior="@string/bottom_sheet_behavior"
      

      如果没有,则使用

      app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
      

      【讨论】:

        【解决方案7】:

        如果您使用的是 androidX,那么您的根布局应该是:

        <androidx.coordinatorlayout.widget.CoordinatorLayout
        

        bottom_sheet 的布局应该包括:

        app:layout_behavior="@string/bottom_sheet_behavior"
        

        否则,如果您不使用 androidx 或使用支持库,那么您的根布局应该是:

        <android.support.design.widget.CoordinatorLayout
        

        bottom_sheet 的布局应该包括:

        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
        

        【讨论】:

          【解决方案8】:

          我遇到了类似的问题并最终访问了此页面。

          我正在使用 Android X 上的底部工作表

          我运行我的应用程序并在我点击移动到应用程序中的新片段时收到以下错误消息,并且应用程序因 RuntimeExeception 崩溃:“无法膨胀行为子类 com.google.android.material。 bottomsheet.BottomSheetBehaviour"

          我的布局属性如下:

          <androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
              android:background="@drawable/player_bg"
              app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehaviour"
              app:behavior_hideable="true"
              app:behavior_peekHeight="70dp">
          

          当我收到该错误消息时,我更改了一行,然后应用程序运行了。

          如上所述,由于我使用的是Android X,我从:

          app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehaviour"
          

          我将这一行改为:

          app:layout_behavior="@string/bottom_sheet_behavior"
          

          这一更改意味着代码对我有用,没有崩溃,并且新的 Fragment 按预期显示。

          【讨论】:

            【解决方案9】:

            如果您使用的是 Androidx Jetpack 依赖项,那么

            替换

            app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
            

            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-02-24
              • 2020-11-26
              • 1970-01-01
              • 1970-01-01
              • 2015-12-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多