From 258af731f628ffcb113964685cbab80be3937174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 28 Jun 2026 12:39:34 +0800 Subject: [PATCH] Fix NPE in update download dialog when progress is null The download progress indicator passed a deferred lambda `progress = { progress!! }` reading UpdateState.downloadProgress (MutableState). Material3 invokes the lambda during layout/draw, outside the composition snapshot that guarded it, so it re-read the live state. ApkDownloader sets the value to null when the response has no content length (total <= 0), and it can also flip while the screen is being torn down, causing the `!!` to throw NPE. Capture the value into an immutable local before branching so the lambda closes over a non-null Float instead of re-reading live state. --- .../main/java/io/nekohasekai/sfa/compose/MainActivity.kt | 9 +++++---- .../sfa/compose/screen/settings/AppSettingsScreen.kt | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/nekohasekai/sfa/compose/MainActivity.kt b/app/src/main/java/io/nekohasekai/sfa/compose/MainActivity.kt index 1af987a..ab08069 100644 --- a/app/src/main/java/io/nekohasekai/sfa/compose/MainActivity.kt +++ b/app/src/main/java/io/nekohasekai/sfa/compose/MainActivity.kt @@ -665,16 +665,17 @@ class MainActivity : ) } else { val progress by UpdateState.downloadProgress + val progressValue = progress Column { - if (progress != null) { - Text("${stringResource(R.string.downloading)} ${(progress!! * 100).toInt()}%") + if (progressValue != null) { + Text("${stringResource(R.string.downloading)} ${(progressValue * 100).toInt()}%") } else { Text(stringResource(R.string.downloading)) } Spacer(modifier = Modifier.height(8.dp)) - if (progress != null) { + if (progressValue != null) { LinearProgressIndicator( - progress = { progress!! }, + progress = { progressValue }, modifier = Modifier.fillMaxWidth(), ) } else { diff --git a/app/src/main/java/io/nekohasekai/sfa/compose/screen/settings/AppSettingsScreen.kt b/app/src/main/java/io/nekohasekai/sfa/compose/screen/settings/AppSettingsScreen.kt index d98651e..5fc0613 100644 --- a/app/src/main/java/io/nekohasekai/sfa/compose/screen/settings/AppSettingsScreen.kt +++ b/app/src/main/java/io/nekohasekai/sfa/compose/screen/settings/AppSettingsScreen.kt @@ -281,16 +281,17 @@ fun AppSettingsScreen( ) } else { val progress by UpdateState.downloadProgress + val progressValue = progress Column { - if (progress != null) { - Text("${stringResource(R.string.downloading)} ${(progress!! * 100).toInt()}%") + if (progressValue != null) { + Text("${stringResource(R.string.downloading)} ${(progressValue * 100).toInt()}%") } else { Text(stringResource(R.string.downloading)) } Spacer(modifier = Modifier.height(8.dp)) - if (progress != null) { + if (progressValue != null) { LinearProgressIndicator( - progress = { progress!! }, + progress = { progressValue }, modifier = Modifier.fillMaxWidth(), ) } else {