无法从 xml 文件扩充 RelativeLayout

Unable to inflate RelativeLayout from xml file

我正在尝试构建类似 BottomSheet 的行为,因此我构建了一个应该从底部滑入的 RelativeLayout。我将它添加到底部的主 xml 文件中,在编辑器中它已经显示出来,但是当我尝试它时,我的应用程序崩溃并且显示此错误消息:

2020-01-09 11:17:10.841 16474-16474/..app.debug E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ..app.debug, PID: 16474
    java.lang.RuntimeException: Unable to start activity ComponentInfo{..app.debug/..core.activity.orderbook.OrderbookActivity}: android.view.InflateException: Binary XML file line #44: Binary XML file line #44: Error inflating class ...orderbook.bottom_menu.OrderbookFilterMenu
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: android.view.InflateException: Binary XML file line #44: Binary XML file line #44: Error inflating class ...orderbook.bottom_menu.OrderbookFilterMenu
     Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class ...module.orderbook.bottom_menu.OrderbookFilterMenu
     Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
        at java.lang.Class.getConstructor0(Class.java:2204)
        at java.lang.Class.getConstructor(Class.java:1683)
        at android.view.LayoutInflater.createView(LayoutInflater.java:618)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at ...module.orderbook.OrderbookFragment.createView(OrderbookFragment.kt:39)
        at ...module_base.fragment.BaseFragment.onCreateView(BaseFragment.java:57)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
        at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
        at android.app.Activity.performStart(Activity.java:6696)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
        at android.os.Handler.dispatchMessage(Handler.java:102)

这是我的主要 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/orderbook_constraint_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/orderbook_recyclerview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="0dp"
        android:layout_weight="1"
        android:background="@color/background_neutral_primary"
        android:overScrollMode="never"
        android:scrollbars="none"
        app:layout_constraintBottom_toTopOf="@id/recyclerview_anchor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent"
        tools:visibility="visible" />

    <Space
        android:id="@+id/recyclerview_anchor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        app:layout_constraintEnd_toEndOf="@+id/orderbook_action_bar"
        app:layout_constraintStart_toStartOf="@+id/orderbook_action_bar"
        app:layout_constraintTop_toTopOf="@+id/orderbook_action_bar" />

    <...ActionBarView
        android:id="@+id/orderbook_action_bar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:background="@color/dev" />

    <...OrderbookFilterMenu
        android:id="@+id/filter_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="parent"
        />

    <...ContextMenu
        android:id="@+id/context_menu"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:visibility="gone"/>


</androidx.constraintlayout.widget.ConstraintLayout>

这是来自底部菜单的片段 (OrderbookFilterMenu)

class FilterBottomSheetFragment : BottomSheetDialogFragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.view_orderbook_bottom_menu_filter, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initViews()
        setupListeners()
    }

以及我的菜单布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:padding="16dp"
    android:background="@drawable/bottomsheet_rounded">


    <...ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:text="@string/order_filter_bottom_menu_header"
            android:textColor="@color/text_primary"
            android:textStyle="bold"/>

        <...FontCheckBox
            android:id="@+id/all_orders_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/checkbox_for_dark_background"
            android:checked="false"
            android:layout_marginStart="32dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/filter_all_text"
            app:layout_constraintBottom_toBottomOf="@id/filter_all_text"/>

        <TextView
            android:id="@+id/filter_all_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/order_filter_all"
            android:textColor="@color/text_primary"
            android:layout_marginTop="16dp"
            app:layout_constraintStart_toEndOf="@+id/all_orders_checkbox"
            app:layout_constraintTop_toBottomOf="@+id/header"
            android:layout_marginStart="8dp"/>

        <...FontCheckBox
            android:id="@+id/open_orders_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/checkbox_for_dark_background"
            android:checked="false"
            android:layout_marginStart="64dp"
            app:layout_constraintTop_toTopOf="@+id/filter_opened_text"
            app:layout_constraintBottom_toBottomOf="@id/filter_opened_text"
            app:layout_constraintStart_toStartOf="parent"/>

        <TextView
            android:id="@+id/filter_opened_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/order_filter_opened_orders"
            android:textColor="@color/text_primary"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toBottomOf="@+id/filter_all_text"
            app:layout_constraintStart_toEndOf="@+id/open_orders_checkbox"/>

        <...FontCheckBox
            android:id="@+id/executed_orders_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/checkbox_for_dark_background"
            android:checked="false"
            app:layout_constraintStart_toStartOf="@+id/open_orders_checkbox"
            app:layout_constraintTop_toTopOf="@+id/filter_executed_text"
            app:layout_constraintBottom_toBottomOf="@+id/filter_executed_text"/>

        <TextView
            android:id="@+id/filter_executed_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/order_filter_executed"
            android:textColor="@color/text_primary"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            app:layout_constraintStart_toEndOf="@+id/executed_orders_checkbox"
            app:layout_constraintTop_toBottomOf="@+id/filter_opened_text"/>

        <...FontCheckBox
            android:id="@+id/canceled_orders_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/checkbox_for_dark_background"
            android:checked="false"
            app:layout_constraintStart_toStartOf="@+id/executed_orders_checkbox"
            app:layout_constraintTop_toTopOf="@+id/filter_canceled_text"
            app:layout_constraintBottom_toBottomOf="@+id/filter_canceled_text"/>

        <TextView
            android:id="@+id/filter_canceled_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/order_filter_canceled"
            android:textColor="@color/text_primary"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            app:layout_constraintStart_toEndOf="@+id/canceled_orders_checkbox"
            app:layout_constraintTop_toBottomOf="@+id/filter_executed_text"/>

        <...CoreFontButton
            android:id="@+id/filter_confirm_button"
            style="@style/button_full_primary"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="16dp"
            android:background="@drawable/button_core_rounded_full_primary"
            android:text="@string/uebernehmen"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/filter_canceled_text"
            app:layout_constraintBottom_toBottomOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

</RelativeLayout>

我不明白他为什么不能夸大这个观点。有人有解决方案的想法吗?

我找到了答案。

我的 OrderbookFilterMenu 有一个这样的构造函数

class OrderbookFilterMenu(context: Context) : RelativeLayout(context)

但显然相对布局需要这样的构造函数

class OrderbookFilterMenu(context: Context, attr: AttributeSet) : RelativeLayout(context, attr)

抱歉没有提供足够的信息,但感谢您的宝贵时间。