由于按下最右邊的按鈕查看打開的應用程序或讓手機進入睡眠狀態,我試圖讓程序在調用 onPause 和 onStop 后正確恢復。我不熟悉正確的電話是什么。我已經查找了一些關于此的信息,但我發現的只是人們說在 onPause 中調用 super.onPause,在 onResume 中調用 super.onResume,然后編寫我的代碼。然而,這段代碼不是我的,并且在恢復時崩潰并出現錯誤:java.lang.RuntimeException: Unable to resume activity {org.tensorflow.lite.examples.detection/org.tensorflow.lite.examples.detection.DetectorActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.startPreview()' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.startPreview()' on a null object reference代碼可在以下位置找到: https: //github.com/tensorflow/examples/blob/master/lite/examples/object_detection/android/app/src/main/java/org/tensorflow/lite/examples/detection/LegacyCameraConnectionFragment .java關于這一切, 我有兩個問題:我可以通過注釋掉第 185-187 行讓相機正確重啟,但我不知道這是否是正確的方法。評論?想法?即使這有效,主要活動也不會重新啟動。這是這里的實際主要問題。我還認為這段代碼一定有問題,但無法弄清楚它是什么。關于如何解決它的任何建議?主要活動代碼鏈接: https://github.com/tensorflow/examples/blob/master/lite/examples/object_detection/android/app/src/main/java/org/tensorflow/lite/examples/detection/CameraActivity.java
1 回答

倚天杖
TA貢獻1828條經驗 獲得超3個贊
由于某種原因,我在問題中所做和提到的更改并沒有解決問題。我想這與調用 onStop 后未正確設置回調或新的后臺線程改變了什么有關?
為了修復它,我添加了
textureView.getSurfaceTextureListener().onSurfaceTextureAvailable(textureView.getSurfaceTexture(), textureView.getWidth(), textureView.getHeight());
在 Legacy... .java 文件的第 152 行之前。