簡體   English   中英

指定為非空的參數是 null 和 BottomSheetDialog

[英]Parameter specified as non-null is null with BottomSheetDialog

當沒有可用的互聯網時,我正在嘗試顯示 bottomSheetDialog。 我使用的代碼是

    private fun setupBottomSheetDialog() {
    bottomSheetDialog =
        object : BottomSheetDialog(requireContext(), R.style.BottomSheetDialogTheme) {
            override fun onCreate(savedInstanceState: Bundle) {
                super.onCreate(savedInstanceState)
                setOnKeyListener { _: DialogInterface?, keyCode: Int, event: KeyEvent ->
                    if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) {
                        // Back key is pressed
                        bottomSheetDialog.dismiss() // Optional
                        requireActivity().moveTaskToBack(true) //exit the app when press back
                        requireActivity().finish()
                        return@setOnKeyListener true
                    }
                    true
                }
            }
        }
    bottomSheetDialog.setContentView(R.layout.bottomsheet_no_internet)
    bottomSheetDialog.setCancelable(false)
}

@SuppressLint("NotifyDataSetChanged")
private fun showBottomSheetDialog() {
    bottomSheetDialog = BottomSheetDialog(requireContext(), R.style.BottomSheetDialogTheme)
    bottomSheetDialog.setContentView(R.layout.bottomsheet_no_internet)
    if (CheckNetwork.isInternetAvailable(requireActivity())) {
        bottomSheetDialog.dismiss()
    } else {
        setupBottomSheetDialog()
    }
    /* Try Again Button */
    val buttonNoInternet = bottomSheetDialog.findViewById<Button>(R.id.buttonAgain)
    buttonNoInternet?.setOnClickListener {
        if (CheckNetwork.isInternetAvailable(requireActivity())) {
            adapter.notifyDataSetChanged()
            bottomSheetDialog.dismiss()
        } else {
            bottomSheetDialog.dismiss()
            adapter.notifyDataSetChanged()
            bottomSheetDialog.show()
        }
    }
}

    override fun onResume() {
    super.onResume()
    if (CheckNetwork.isInternetAvailable(requireActivity())) {
        bottomSheetDialog.dismiss()
    } else {
        bottomSheetDialog.show()
    }
}

我遇到的問題在於onResume() mehoud。 我得到的錯誤信息是

Caused by: java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter savedInstanceState at com.moataz.afternoonhadeeth.ui.view.fragment.HomeFragment$setupBottomSheetDialog$1.onCreate (Unknown Source:2) at android.app.Dialog.dispatchOnCreate(Dialog.java:616) at android.app.Dialog.show(Dialog.java:460) at com.moataz.afternoonhadeeth.ui.view.fragment.HomeFragment .onResume(HomeFragment.kt:143)

和在錯誤消息的頂部

java.lang.RuntimeException: Unable to resume activity {com.moataz.afternoonhadeeth/com.moataz.afternoonhadeeth.ui.view.activity.MainActivity}: java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter savedInstanceState

注意:代碼是否與 Java 一起工作正常。 但是當我將它與 kotlin 一起使用時,它會給我錯誤。 任何想法如何解決這個問題?

好的,我發現了問題。 我應該添加savedInstanceState: Bundle? 在底部對話框中的創建方法中。 如果有人想知道如何在他的應用程序中正確使用 BottomDialog,我將把問題留在這里。 祝你有美好的一天。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM