0

Using BottomSheetDialogFragment in my project always get the error:

android.view.InflateException: Binary XML file line #40: Could not inflate Behavior subclass androidx.coordinatorlayout.widget.bottom sheet behavior
    Caused by: java.lang.RuntimeException: Could not inflate Behavior subclass androidx.coordinatorlayout.widget.bottom sheet behavior

Using the fragment in activity, but always get the error above

button = findViewById(R.id.button)
val modalBottomSheet = ModalBottomSheetFragment()
button.setOnClickListener {
    modalBottomSheet.show(supportFragmentManager, "tag")
}

My fragment:

class ModalBottomSheetFragment : BottomSheetDialogFragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

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

and my fragment layout:

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

    <TextView
            android:id="@+id/hello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="Hello Click me!" />

</LinearLayout>
zeleven
  • 349
  • 4
  • 15

1 Answers1

1

Update your code with bellow code

class  ModalBottomSheetFragment: BottomSheetDialogFragment() {
    @SuppressLint("RestrictedApi")
    override fun setupDialog(dialog: Dialog, style: Int) {

        super.setupDialog(dialog, style)
        //Set the custom view
        val view = LayoutInflater.from(context).inflate(R.layout.fragment_modal_bottom_sheet, null)
        dialog.setContentView(view)
    }
}
Dhaval Solanki
  • 4,093
  • 1
  • 21
  • 33