diff --git a/android/src/main/java/com/lodev09/truesheet/TrueSheetDialog.kt b/android/src/main/java/com/lodev09/truesheet/TrueSheetDialog.kt index cf3e99a..f17a6cd 100644 --- a/android/src/main/java/com/lodev09/truesheet/TrueSheetDialog.kt +++ b/android/src/main/java/com/lodev09/truesheet/TrueSheetDialog.kt @@ -24,11 +24,15 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val private var windowAnimation: Int = 0 // First child of the rootSheetView - private val containerView: ViewGroup - get() = rootSheetView.getChildAt(0) as ViewGroup + private val containerView: ViewGroup? + get() = if (rootSheetView.childCount > 0) { + rootSheetView.getChildAt(0) as? ViewGroup + } else { + null + } - private val sheetContainerView: ViewGroup - get() = rootSheetView.parent as ViewGroup + private val sheetContainerView: ViewGroup? + get() = rootSheetView.parent?.let { it as? ViewGroup } /** * Specify whether the sheet background is dimmed. @@ -70,20 +74,20 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val var backgroundColor: Int = Color.WHITE // 1st child is the content view - val contentView: ViewGroup - get() = containerView.getChildAt(0) as ViewGroup + val contentView: ViewGroup? + get() = containerView?.getChildAt(0) as? ViewGroup // 2nd child is the footer view - val footerView: ViewGroup - get() = containerView.getChildAt(1) as ViewGroup + val footerView: ViewGroup? + get() = containerView?.getChildAt(1) as? ViewGroup var sizes: Array = arrayOf("medium", "large") init { setContentView(rootSheetView) - sheetContainerView.setBackgroundColor(backgroundColor) - sheetContainerView.clipToOutline = true + sheetContainerView?.setBackgroundColor(backgroundColor) + sheetContainerView?.clipToOutline = true // Setup window params to adjust layout based on Keyboard state window?.apply { @@ -131,7 +135,7 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val // Use current background color background.paint.color = backgroundColor - sheetContainerView.background = background + sheetContainerView?.background = background } /** @@ -196,8 +200,10 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val } fun positionFooter() { - footerView.apply { - y = (maxScreenHeight - sheetContainerView.top - footerHeight).toFloat() + footerView?.let { footer -> + sheetContainerView?.let { container -> + footer.y = (maxScreenHeight - container.top - footerHeight).toFloat() + } } } diff --git a/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt b/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt index 90d4023..5627a42 100644 --- a/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt +++ b/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt @@ -178,8 +178,8 @@ class TrueSheetView(context: Context) : // Initialize content UiThreadUtil.runOnUiThread { - setContentHeight(sheetDialog.contentView.height) - setFooterHeight(sheetDialog.footerView.height) + sheetDialog.contentView?.height?.let { setContentHeight(it) } + sheetDialog.footerView?.height?.let { setFooterHeight(it) } if (initialIndex >= 0) { currentSizeIndex = initialIndex