API 15.当我拍摄一张照片后停止相机,转到主屏幕,重新打开我的应用程序并尝试拍摄另一张照片时,我的应用程序崩溃,并收到以下错误:
04-20 12:04:38.437: E/AndroidRuntime(5150): FATAL EXCEPTION: Timer-2
04-20 12:04:38.437: E/AndroidRuntime(5150): java.lang.RuntimeException: Method called after release()
04-20 12:04:38.437: E/AndroidRuntime(5150): at android.hardware.Camera.native_takePicture(Native Method)
04-20 12:04:38.437: E/AndroidRuntime(5150): at android.hardware.Camera.takePicture(Camera.java:947)
04-20 12:04:38.437: E/AndroidRuntime(5150): at android.hardware.Camera.takePicture(Camera.java:892)
04-20 12:04:38.437: E/AndroidRuntime(5150): at com.prism.app.PrismActivity$5.run(PrismActivity.java:167)
04-20 12:04:38.437: E/AndroidRuntime(5150): at java.util.Timer$TimerImpl.run(Timer.java:284)
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
if (mCamera == null) {
try {
mCamera = Camera.open();
mCamera.setPreviewDisplay(holder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (IOException e) {
// error setting preview of camera
}
} else {
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release(); //need to take care of case when app is not closed completely still need to release
mCamera = null;
}
}发布于 2012-04-21 03:40:45
从堆栈跟踪来看,您似乎有一些TimerTask仍计划执行,它使用相机。它会在你关闭相机后触发,因此会出现错误。您需要取消Timer,并且还要准备好在您取消之前最后一个TimerTask可能正在进行。因此,在操作摄像头之前,任务需要检查摄像头是否关闭。
发布于 2012-04-21 03:38:22
本质上,您是在调用mCamera对象的release方法并在其本机代码中释放该对象之后尝试使用该对象。
该对象仍然存在(因此您的mCamera == null计算结果为false),但是如果不创建一个新的对象或再次调用一个获取本机资源的方法,就不能再使用该对象。
https://stackoverflow.com/questions/10252438
复制相似问题