I'm trying to display bottomSheetDialog when there's no internet available. the code that I used is
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()
}
}
the problem that I have is in onResume() mehoud. and the error message that I got is
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)
and In top of error message
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
Note: is code is working fine with Java. But when I use it with kotlin it is give me the error. Any Idea how to solve this?
CodePudding user response:
ok, I have found the problem. I should add savedInstanceState: Bundle?
in on create method in bottomdialog. I will leave the question here if anyone wants to know how to use BottomDialog correctly in his app.
Have a good day.