修复MainActivity停止时CameraX NPE崩溃
1. 升级CameraX版本 1.3.0 → 1.4.1,从根本修复RequestWithCallback.abort() NPE 2. 跳转PayActivity前调用shutdownCamera(),防止in-flight拍照请求触发生命周期异常 3. onResume()顶部增加相机恢复逻辑,通过cameraProviderFuture==null判断是否需要重新初始化 4. shutdownCamera()中将cameraProviderFuture置null,作为相机已停止的可靠标志 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -457,6 +457,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
// 3. 关闭相关资源
|
// 3. 关闭相关资源
|
||||||
imageCapture = null
|
imageCapture = null
|
||||||
imageAnalysis = null
|
imageAnalysis = null
|
||||||
|
cameraProviderFuture = null
|
||||||
|
|
||||||
// 4. 停止副屏更新
|
// 4. 停止副屏更新
|
||||||
isAnalyzing = false
|
isAnalyzing = false
|
||||||
@@ -555,6 +556,10 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
isPageVisible = true
|
isPageVisible = true
|
||||||
|
// shutdownCamera 后 cameraProviderFuture 置为 null,需重新初始化相机
|
||||||
|
if (cameraProviderFuture == null) {
|
||||||
|
setupCamera()
|
||||||
|
}
|
||||||
// 启动定时器
|
// 启动定时器
|
||||||
handler.postDelayed(timeoutRunnable, TIME_OUT)
|
handler.postDelayed(timeoutRunnable, TIME_OUT)
|
||||||
// ToastUtils.showToast("isPageVisible=$isPageVisible")
|
// ToastUtils.showToast("isPageVisible=$isPageVisible")
|
||||||
@@ -797,7 +802,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
userId = null,
|
userId = null,
|
||||||
) {
|
) {
|
||||||
isRefreshPage = false
|
isRefreshPage = false
|
||||||
//presentation?.dismiss()
|
shutdownCamera()
|
||||||
startActivity(Intent(this, PayActivity::class.java).apply {
|
startActivity(Intent(this, PayActivity::class.java).apply {
|
||||||
putExtra(PayActivity.FOOD_INFO, checkedItem!!)
|
putExtra(PayActivity.FOOD_INFO, checkedItem!!)
|
||||||
putExtra(PayActivity.FOOD_EAT_NUM, eatNum)
|
putExtra(PayActivity.FOOD_EAT_NUM, eatNum)
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ material = "1.10.0"
|
|||||||
activity = "1.8.0"
|
activity = "1.8.0"
|
||||||
constraintlayout = "2.1.4"
|
constraintlayout = "2.1.4"
|
||||||
accompanistPermissions = "0.37.3"
|
accompanistPermissions = "0.37.3"
|
||||||
cameraCore = "1.3.0"
|
cameraCore = "1.4.1"
|
||||||
retrofit = "3.0.0"
|
retrofit = "3.0.0"
|
||||||
okhttp = "4.12.0"
|
okhttp = "4.12.0"
|
||||||
timber = "5.0.1"
|
timber = "5.0.1"
|
||||||
|
|||||||
Reference in New Issue
Block a user