1
我有一個活動,我初始化條碼檢測。然後我去到另一個活動時,我按後退按鈕,它觸發此異常的循環:返回到活動後CameraSource觸發空例外
com.example.myapp E/CameraSource: Exception thrown from receiver.
java.lang.NullPointerException
at com.google.android.gms.vision.CameraSource$zzb.run(Unknown Source)
at java.lang.Thread.run(Thread.java:841)
如果我堅持,再轉到其他活動它觸發另一個異常:
com.example.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapp, PID: 22942
java.lang.NullPointerException
at com.google.android.gms.vision.CameraSource$zzb.release(Unknown Source)
at com.google.android.gms.vision.CameraSource.release(Unknown Source)
at com.example.myapp.CameraActivity$3.surfaceDestroyed(CameraActivity.java:184)
at android.view.SurfaceView.updateWindow(SurfaceView.java:586)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:240)
at android.view.View.dispatchWindowVisibilityChanged(View.java:8495)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1537)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6602)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:805)
at android.view.Choreographer.doCallbacks(Choreographer.java:605)
at android.view.Choreographer.doFrame(Choreographer.java:575)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:791)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
CameraSource初始化像這樣的onCreate:
final CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)
.setRequestedFps(15f)
.setAutoFocusEnabled(true)
.build();
mCameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
return;
cameraSource.start(holder);
} catch (IOException e) {
Log.e(getClass().getName(), e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.release();
}
});
活動如果你跳到另一個活動,然後你回到他們,暫停和恢復。在這裏看看如何在Pause上釋放相機並在Resume上重新初始化它。它可能會做的伎倆,但不是100%在你的情況下,它可能是一個不同的問題 - https://developer.android.com/training/basics/activity-lifecycle/pausing.html – Tasos
我把inicialization代碼從'onCreate '到'onResume'並保護相機釋放,因爲你的鏈接說現在沒問題。謝謝。 – Fabricio
意思是說(不是100%確定這是否會有所幫助),但我想它確實:) – Tasos