处理副屏加载失败导致的崩溃问题

This commit is contained in:
zxj
2025-08-11 17:53:20 +08:00
parent fde2cd0544
commit e93550a24b
2 changed files with 15 additions and 12 deletions
@@ -46,7 +46,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
private val viewModel by viewModels<UserViewModel>() private val viewModel by viewModels<UserViewModel>()
private var imageCapture: ImageCapture? = null private var imageCapture: ImageCapture? = null
private var isAnalyzing = true // 控制是否进行图像分析 private var isAnalyzing = true // 控制是否进行图像分析
private lateinit var presentation: SecondaryScreenPresentation private var presentation: SecondaryScreenPresentation? = null
private val executor = Executors.newSingleThreadExecutor() private val executor = Executors.newSingleThreadExecutor()
private lateinit var adapter: GenericItemAdapter<FoodInfo, ItemFoodInfoBinding> private lateinit var adapter: GenericItemAdapter<FoodInfo, ItemFoodInfoBinding>
private var checkedItem: FoodInfo? = null private var checkedItem: FoodInfo? = null
@@ -74,7 +74,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
private fun initData() { private fun initData() {
viewModel.getEquipmentToken() viewModel.getEquipmentToken()
presentation.step1() presentation?.step1()
} }
private fun initView() { private fun initView() {
@@ -106,7 +106,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
position: Int, position: Int,
id: Long id: Long
) { ) {
presentation.updateMealPickupMode(position) presentation?.updateMealPickupMode(position)
} }
override fun onNothingSelected(parent: AdapterView<*>?) {} override fun onNothingSelected(parent: AdapterView<*>?) {}
@@ -129,7 +129,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
fun updateCurrentFood(foodInfo: FoodInfo?) { fun updateCurrentFood(foodInfo: FoodInfo?) {
if (foodInfo != null) { if (foodInfo != null) {
presentation.updateFood(foodInfo) presentation?.updateFood(foodInfo)
binding.tvFoodName.text = foodInfo.foodName binding.tvFoodName.text = foodInfo.foodName
binding.previewView.visibility = View.GONE binding.previewView.visibility = View.GONE
binding.ivImg.visibility = View.VISIBLE binding.ivImg.visibility = View.VISIBLE
@@ -137,7 +137,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
if (TextUtils.isEmpty(imgUrl)) { if (TextUtils.isEmpty(imgUrl)) {
takePhoto { imgUri -> takePhoto { imgUri ->
GlideUtils.loadRoundCornerImage(this, imgUri, binding.ivImg, 12) GlideUtils.loadRoundCornerImage(this, imgUri, binding.ivImg, 12)
presentation.updateFood(foodInfo.copy(imgUrl = imgUri.toString())) presentation?.updateFood(foodInfo.copy(imgUrl = imgUri.toString()))
} }
} else { } else {
GlideUtils.loadRoundCornerImage(this, foodInfo.imgUrl, binding.ivImg, 12) GlideUtils.loadRoundCornerImage(this, foodInfo.imgUrl, binding.ivImg, 12)
@@ -163,7 +163,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
} }
SensorScaleUtils.startScale { weight -> SensorScaleUtils.startScale { weight ->
Timber.d("registerDataChange weight = $weight") Timber.d("registerDataChange weight = $weight")
presentation.updateWeight(weight) presentation?.updateWeight(weight)
if (weight > 0.0) { if (weight > 0.0) {
if (canIdentify) { if (canIdentify) {
canIdentify = false canIdentify = false
@@ -222,7 +222,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
// 仅在步骤1时传递 // 仅在步骤1时传递
if (isAnalyzing) { if (isAnalyzing) {
val bitmap = imageProxy.toBitmap() val bitmap = imageProxy.toBitmap()
presentation.updateImage(bitmap) // 更新副屏 presentation?.updateImage(bitmap) // 更新副屏
} }
imageProxy.close() // 必须关闭以释放资源 imageProxy.close() // 必须关闭以释放资源
} }
@@ -262,9 +262,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
if (isAnalyzing) return if (isAnalyzing) return
isAnalyzing = true isAnalyzing = true
imageAnalysis?.setAnalyzer(executor) { imageProxy -> imageAnalysis?.setAnalyzer(executor) { imageProxy ->
if (presentation.currentStep == 1) { if (presentation?.currentStep == 1) {
val bitmap = imageProxy.toBitmap() val bitmap = imageProxy.toBitmap()
presentation.updateImage(bitmap) presentation?.updateImage(bitmap)
} }
imageProxy.close() imageProxy.close()
} }
@@ -356,19 +356,21 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
viewModel = viewModel, viewModel = viewModel,
recognizeViewModel = viewModels<RecognizeViewModel>().value recognizeViewModel = viewModels<RecognizeViewModel>().value
) )
presentation.setStepChangeCallback { step -> presentation!!.setStepChangeCallback { step ->
if (step == 1) { if (step == 1) {
updateCurrentFood(null) updateCurrentFood(null)
bottomSheetDialog?.dismiss() bottomSheetDialog?.dismiss()
} }
} }
presentation.show() presentation!!.show()
} else {
ToastUtils.showToast("获取副屏失败")
} }
} }
override fun onDestroy() { override fun onDestroy() {
SensorScaleUtils.closeScale() SensorScaleUtils.closeScale()
presentation.dismiss() presentation?.dismiss()
super.onDestroy() super.onDestroy()
} }
} }
@@ -121,6 +121,7 @@
android:layout_height="486dp" android:layout_height="486dp"
android:layout_gravity="bottom" android:layout_gravity="bottom"
android:background="@drawable/rounded_border_transparent" android:background="@drawable/rounded_border_transparent"
android:orientation="vertical"
android:gravity="center"> android:gravity="center">
<TextView <TextView