Fix UninitializedPropertyAccessException in PolycentricBackupActivity

- Move export bundle creation before try-catch block to ensure _exportBundle is initialized
- Fix crash when accessing _exportBundle in error logging before it's been set
- Use local bundle variable in catch block instead of accessing uninitialized _exportBundle
- Ensure _exportBundle is always available for UI operations and error handling

This resolves the crash that occurred when QR code generation failed and the app
tried to access the uninitialized _exportBundle property.
This commit is contained in:
Trevor
2025-08-19 11:21:34 -05:00
parent 6e58107a5e
commit 8f5d90a1c8
@@ -84,11 +84,13 @@ class PolycentricBackupActivity : AppCompatActivity() {
_buttonExportFile.visibility = View.INVISIBLE
lifecycleScope.launch {
try {
val pair = withContext(Dispatchers.IO) {
val bundle = createExportBundle()
// Create export bundle first
val bundle = withContext(Dispatchers.IO) { createExportBundle() }
_exportBundle = bundle
Logger.i(TAG, "Export bundle created, length: ${bundle.length}")
try {
val pair = withContext(Dispatchers.IO) {
// Check if bundle is suitable for QR code
if (!isContentSuitableForQRCode(bundle)) {
throw Exception("Data too big for QR code generation")
@@ -101,7 +103,6 @@ class PolycentricBackupActivity : AppCompatActivity() {
Pair(bundle, qr)
}
_exportBundle = pair.first
_imageQR.setImageBitmap(pair.second)
_imageQR.visibility = View.VISIBLE
_textQR.visibility = View.VISIBLE
@@ -115,11 +116,8 @@ class PolycentricBackupActivity : AppCompatActivity() {
startActivity(intent)
}
} catch (e: Exception) {
val byteSize = _exportBundle.toByteArray(Charsets.UTF_8).size
Logger.e(TAG, "QR code generation failed. Bundle length: ${_exportBundle.length} chars, ${byteSize} bytes, Error: ${e.message}", e)
// Show the export bundle text even if QR code generation fails
_exportBundle = withContext(Dispatchers.IO) { createExportBundle() }
val byteSize = bundle.toByteArray(Charsets.UTF_8).size
Logger.e(TAG, "QR code generation failed. Bundle length: ${bundle.length} chars, ${byteSize} bytes, Error: ${e.message}", e)
// Show file export button when QR code is too large
if (e.message?.contains("Data too big") == true) {