BottomSheetDialogFragment всегда получить не может раздувать ошибку поведения - PullRequest
0 голосов
/ 05 июня 2019

Использование BottomSheetDialogFragment в моем проекте всегда выдает ошибку:

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

Использование фрагмента в активности, но всегда выдает ошибку выше

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

Мой фрагмент:

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

и мой фрагментный макет:

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

1 Ответ

1 голос
/ 05 июня 2019

Обновите код с помощью приведенного ниже кода

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