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

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 var imageCapture: ImageCapture? = null
private var isAnalyzing = true // 控制是否进行图像分析
private lateinit var presentation: SecondaryScreenPresentation
private var presentation: SecondaryScreenPresentation? = null
private val executor = Executors.newSingleThreadExecutor()
private lateinit var adapter: GenericItemAdapter<FoodInfo, ItemFoodInfoBinding>
private var checkedItem: FoodInfo? = null
@@ -74,7 +74,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
private fun initData() {
viewModel.getEquipmentToken()
presentation.step1()
presentation?.step1()
}
private fun initView() {
@@ -106,7 +106,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
position: Int,
id: Long
) {
presentation.updateMealPickupMode(position)
presentation?.updateMealPickupMode(position)
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
@@ -129,7 +129,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
fun updateCurrentFood(foodInfo: FoodInfo?) {
if (foodInfo != null) {
presentation.updateFood(foodInfo)
presentation?.updateFood(foodInfo)
binding.tvFoodName.text = foodInfo.foodName
binding.previewView.visibility = View.GONE
binding.ivImg.visibility = View.VISIBLE
@@ -137,7 +137,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
if (TextUtils.isEmpty(imgUrl)) {
takePhoto { imgUri ->
GlideUtils.loadRoundCornerImage(this, imgUri, binding.ivImg, 12)
presentation.updateFood(foodInfo.copy(imgUrl = imgUri.toString()))
presentation?.updateFood(foodInfo.copy(imgUrl = imgUri.toString()))
}
} else {
GlideUtils.loadRoundCornerImage(this, foodInfo.imgUrl, binding.ivImg, 12)
@@ -163,7 +163,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}
SensorScaleUtils.startScale { weight ->
Timber.d("registerDataChange weight = $weight")
presentation.updateWeight(weight)
presentation?.updateWeight(weight)
if (weight > 0.0) {
if (canIdentify) {
canIdentify = false
@@ -222,7 +222,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
// 仅在步骤1时传递
if (isAnalyzing) {
val bitmap = imageProxy.toBitmap()
presentation.updateImage(bitmap) // 更新副屏
presentation?.updateImage(bitmap) // 更新副屏
}
imageProxy.close() // 必须关闭以释放资源
}
@@ -262,9 +262,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
if (isAnalyzing) return
isAnalyzing = true
imageAnalysis?.setAnalyzer(executor) { imageProxy ->
if (presentation.currentStep == 1) {
if (presentation?.currentStep == 1) {
val bitmap = imageProxy.toBitmap()
presentation.updateImage(bitmap)
presentation?.updateImage(bitmap)
}
imageProxy.close()
}
@@ -356,19 +356,21 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
viewModel = viewModel,
recognizeViewModel = viewModels<RecognizeViewModel>().value
)
presentation.setStepChangeCallback { step ->
presentation!!.setStepChangeCallback { step ->
if (step == 1) {
updateCurrentFood(null)
bottomSheetDialog?.dismiss()
}
}
presentation.show()
presentation!!.show()
} else {
ToastUtils.showToast("获取副屏失败")
}
}
override fun onDestroy() {
SensorScaleUtils.closeScale()
presentation.dismiss()
presentation?.dismiss()
super.onDestroy()
}
}