Skip to content

Commit f78dcd4

Browse files
fix: add android viewgroup null check (#146)
* fix: added null check on Android ViewGroup * chore: improved null check on Android ViewGroup * chore: minor formatting/linting * chore: minor formatting/linting
1 parent ee22df7 commit f78dcd4

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

android/src/main/java/com/lodev09/truesheet/TrueSheetDialog.kt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
2424
private var windowAnimation: Int = 0
2525

2626
// First child of the rootSheetView
27-
private val containerView: ViewGroup
28-
get() = rootSheetView.getChildAt(0) as ViewGroup
27+
private val containerView: ViewGroup?
28+
get() = if (rootSheetView.childCount > 0) {
29+
rootSheetView.getChildAt(0) as? ViewGroup
30+
} else {
31+
null
32+
}
2933

30-
private val sheetContainerView: ViewGroup
31-
get() = rootSheetView.parent as ViewGroup
34+
private val sheetContainerView: ViewGroup?
35+
get() = rootSheetView.parent?.let { it as? ViewGroup }
3236

3337
/**
3438
* Specify whether the sheet background is dimmed.
@@ -70,20 +74,20 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
7074
var backgroundColor: Int = Color.WHITE
7175

7276
// 1st child is the content view
73-
val contentView: ViewGroup
74-
get() = containerView.getChildAt(0) as ViewGroup
77+
val contentView: ViewGroup?
78+
get() = containerView?.getChildAt(0) as? ViewGroup
7579

7680
// 2nd child is the footer view
77-
val footerView: ViewGroup
78-
get() = containerView.getChildAt(1) as ViewGroup
81+
val footerView: ViewGroup?
82+
get() = containerView?.getChildAt(1) as? ViewGroup
7983

8084
var sizes: Array<Any> = arrayOf("medium", "large")
8185

8286
init {
8387
setContentView(rootSheetView)
8488

85-
sheetContainerView.setBackgroundColor(backgroundColor)
86-
sheetContainerView.clipToOutline = true
89+
sheetContainerView?.setBackgroundColor(backgroundColor)
90+
sheetContainerView?.clipToOutline = true
8791

8892
// Setup window params to adjust layout based on Keyboard state
8993
window?.apply {
@@ -131,7 +135,7 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
131135

132136
// Use current background color
133137
background.paint.color = backgroundColor
134-
sheetContainerView.background = background
138+
sheetContainerView?.background = background
135139
}
136140

137141
/**
@@ -196,8 +200,10 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
196200
}
197201

198202
fun positionFooter() {
199-
footerView.apply {
200-
y = (maxScreenHeight - sheetContainerView.top - footerHeight).toFloat()
203+
footerView?.let { footer ->
204+
sheetContainerView?.let { container ->
205+
footer.y = (maxScreenHeight - container.top - footerHeight).toFloat()
206+
}
201207
}
202208
}
203209

android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ class TrueSheetView(context: Context) :
178178

179179
// Initialize content
180180
UiThreadUtil.runOnUiThread {
181-
setContentHeight(sheetDialog.contentView.height)
182-
setFooterHeight(sheetDialog.footerView.height)
181+
sheetDialog.contentView?.height?.let { setContentHeight(it) }
182+
sheetDialog.footerView?.height?.let { setFooterHeight(it) }
183183

184184
if (initialIndex >= 0) {
185185
currentSizeIndex = initialIndex

0 commit comments

Comments
 (0)