java.lang.RuntimeException:在release()之后调用的方法

  1. 如果我不使用 mCamera.release();surfaceDestroyed(….)然后无法再启动CameraActivity从另一个活动 [总之得到不幸的应用程序已经停止 ]错误, 即使不释放相机 ,但如果我点击主页button [ 从CameraActivity ],然后再次启动我的应用程序 ,没有得到任何错误(简单的工作正常,打开CameraActivity没有任何问题)

  2. 如果我使用 * mCamera.release(); *在surfaceDestroyed(….)然后能够从另一个活动再次启动CameraActivity释放相机 ,但是当我点击主页button,然后再次启动我的应用程序,越来越不幸的应用程序已停止 ….:

但是我希望两件事情一起工作 (首先, 从CameraActivity点击首页,然后再次从CameraActivity启动应用程序 – 没有任何错误),(其次, 从另一个活动启动相机 – 没有任何错误)

就像我写的,这两样东西都在为我工作,但不是在一起…..

行号33是:

Camera.Parameters parameters = mCamera.getParameters(); 

完成日志:

 12-30 12:18:58.070: W/dalvikvm(14822): threadid=1: thread exiting with uncaught exception (group=0x41ef72a0) 12-30 12:18:58.080: E/AndroidRuntime(14822): FATAL EXCEPTION: main 12-30 12:18:58.080: E/AndroidRuntime(14822): java.lang.RuntimeException: Method called after release() 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.hardware.Camera.native_getParameters(Native Method) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.hardware.Camera.getParameters(Camera.java:1487) 12-30 12:18:58.080: E/AndroidRuntime(14822): at app.micheal.camr.PreviewSurface.surfaceCreated(PreviewSurface.java:33) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.SurfaceView.updateWindow(SurfaceView.java:609) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:235) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.View.dispatchWindowVisibilityChanged(View.java:7686) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1339) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1114) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4520) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.Choreographer.doCallbacks(Choreographer.java:555) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.Choreographer.doFrame(Choreographer.java:525) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.os.Handler.handleCallback(Handler.java:615) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.os.Handler.dispatchMessage(Handler.java:92) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.os.Looper.loop(Looper.java:137) 12-30 12:18:58.080: E/AndroidRuntime(14822): at android.app.ActivityThread.main(ActivityThread.java:4921) 12-30 12:18:58.080: E/AndroidRuntime(14822): at java.lang.reflect.Method.invokeNative(Native Method) 12-30 12:18:58.080: E/AndroidRuntime(14822): at java.lang.reflect.Method.invoke(Method.java:511) 12-30 12:18:58.080: E/AndroidRuntime(14822): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036) 12-30 12:18:58.080: E/AndroidRuntime(14822): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803) 12-30 12:18:58.080: E/AndroidRuntime(14822): at dalvik.system.NativeStart.main(Native Method) 12-30 12:19:08.095: I/Process(14822): Sending signal. PID: 14822 SIG: 9 

PreviewSurface.java:

 public class PreviewSurface extends SurfaceView implements SurfaceHolder.Callback { public static final String LOG_TAG = "CameraPreview"; private SurfaceHolder mSurfaceHolder; private Camera mCamera; Camera.Parameters parameters = null ; // Constructor that obtains context and camera @SuppressWarnings("deprecation") public PreviewSurface(Context context, Camera camera) { super(context); this.mCamera = camera; this.mSurfaceHolder = this.getHolder(); this.mSurfaceHolder.addCallback(this); this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); this.mSurfaceHolder.setFixedSize(100, 100); } @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { try { parameters = mCamera.getParameters(); if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { parameters.set("orientation", "portrait"); mCamera.setDisplayOrientation(90); parameters.setRotation(90); mCamera.setPreviewDisplay(surfaceHolder); mCamera.startPreview(); } else { // This is an undocumented although widely known feature parameters.set("orientation", "landscape"); // For Android 2.2 and above mCamera.setDisplayOrientation(0); // Uncomment for Android 2.0 and above parameters.setRotation(0); } mCamera.setPreviewDisplay(surfaceHolder); mCamera.startPreview(); } catch (IOException e) { // left blank for now } } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { mCamera.stopPreview(); mCamera.release(); } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) { try { parameters = mCamera.getParameters(); if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { parameters.set("orientation", "portrait"); mCamera.setDisplayOrientation(90); parameters.setRotation(90); } else { // This is an undocumented although widely known feature parameters.set("orientation", "landscape"); // For Android 2.2 and above mCamera.setDisplayOrientation(0); // Uncomment for Android 2.0 and above parameters.setRotation(0); } mCamera.setPreviewDisplay(surfaceHolder); mCamera.startPreview(); } catch (IOException e) { // left blank for now } } } 

Solutions Collecting From Web of "java.lang.RuntimeException:在release()之后调用的方法"

试试这个,用我的代码replace你的代码,我想这会帮助你, 我还没试过,但是看起来它会解决你的错误

  @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { this.getHolder().removeCallback(this); mCamera.stopPreview(); mCamera.release(); } 

让我知道尽快…

这是因为当你按下主页button时,你的当前活动将处于onStop模式,当你再次回来时,它将从onRestart状态开始。 不是从PreviewSurface的构造函数中获取相机的位置。 所以,在没有人试图让相同的相机保持不变的情况下。

但是,当你从你开始另一个活动目前正在使用一个SurfaceView的Activity,这将从构造函数开始&将试图获得一个已被您的前一个活动已经举行的相同的相机。

我认为你的mCamera是空的,看起来像你忘记了初始化相机

  mCamera = Camera.open(); 

看这个答案

 public void surfaceCreated(SurfaceHolder surfaceHolder) { try { // here you should set open camera . mCamera= Camera.open(); Camera.Parameters parameters = mCamera.getParameters(); if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { parameters.set("orientation", "portrait"); mCamera.setDisplayOrientation(90); parameters.setRotation(90); mCamera.setPreviewDisplay(surfaceHolder); mCamera.startPreview(); } else { // This is an undocumented although widely known feature parameters.set("orientation", "landscape"); // For Android 2.2 and above mCamera.setDisplayOrientation(0); // Uncomment for Android 2.0 and above parameters.setRotation(0); } mCamera.setPreviewDisplay(surfaceHolder); mCamera.startPreview(); } catch (IOException e) { // left blank for now } } 

而不是重写onPause()和onResume(),重写onStop()和onRestart()。 在活动生命周期中,当活动不可见并且下一个生命周期方法调用是onRestart()时,将调用onStop()。 看看下面的代码。

 @Override protected void onStop() { super.onStop(); try { m_camera.stopPreview(); m_camera.release(); preview.removeView(m_CameraPreview); } catch(Exception e) { e.printStackTrace(); } } 

m_CameraPreview是类的对象,如下所示:public class CameraSurfaceView extends SurfaceView implements Callback

以下是onRestart方法:

  @Override protected void onRestart() { super.onRestart(); m_camera=getCameraInstance();//Initialize the camera in your own way m_CameraPreview = new CameraSurfaceView(this, m_camera); preview = (FrameLayout)findViewById(R.id.camera_preview); preview.addView(this.m_CameraPreview); /* *camera_preview is the id of the framelayout defined in xml file and preview is *the instance of FrameLayout. */ } 

框架布局将保留到前一个摄像头实例,并且除了新的摄像头对象外,还将创build其“surfaceviewcallback”,从而创build竞争条件。 因此,您需要在onStop()中释放它,并在onRestart()中重新初始化。 希望这可以帮助。

 in OnPause stop the camera preview and release the camera @Override public void onPause() { super.onPause(); camera.stopPreview(); camera.release(); camera = null; } in onResume initialize the camera @Override public void onResume() { super.onResume(); camera = Camera.open(); surfaceView = (SurfaceView)view.findViewById(R.id.surfaceView); surfaceView.getHolder().addCallback(this); surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL); }