0

当我使用意图离开活动转到另一个活动时,surfaceView 类崩溃。如何将此活动留给另一个活动而不让它崩溃?

我使用这个意图离开这个类并打开另一个名为 CameraView 类的活动:

  Intent intent = new Intent(this, CameraView.class);
  startActivity(intent);

来自 LogCat 的堆栈跟踪:

04-08 21:42:46.620: W/IInputConnectionWrapper(25016): finishComposingText on inactive InputConnection
04-08 21:42:46.630: E/SurfaceView(25016): NullPointerException while updating window.
mSession = android.view.IWindowSession$Stub$Proxy@41739aa8, mWindow = android.view.SurfaceView$MyWindow@41743978
04-08 21:42:46.630: E/SurfaceView(25016): Unexpected NullPointerException.
04-08 21:42:46.630: D/AndroidRuntime(25016): Shutting down VM
04-08 21:42:46.630: W/dalvikvm(25016): threadid=1: thread exiting with uncaught exception (group=0x40aca9d8)
04-08 21:42:46.640: E/AndroidRuntime(25016): FATAL EXCEPTION: main
04-08 21:42:46.640: E/AndroidRuntime(25016): java.lang.NullPointerException
04-08 21:42:46.640: E/AndroidRuntime(25016):    at android.view.SurfaceView.updateWindow(SurfaceView.java:578)
04-08 21:42:46.640: E/AndroidRuntime(25016):    at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:226)

surfaceView 活动代码:

 class Preview extends SurfaceView implements SurfaceHolder.Callback {
        SurfaceHolder mHolder;
        DrawOnTop mDrawOnTop;


        Preview(Context context, DrawOnTop drawOnTop) {
            super(context);

            mDrawOnTop = drawOnTop;
            mFinished = false;

            // Install a SurfaceHolder.Callback so we get notified when the
            // underlying surface is created and destroyed.
            mHolder = getHolder();
            mHolder.addCallback(this);
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }

        public void surfaceCreated(SurfaceHolder holder) {

            try {
               mCamera = Camera.open();
               mCamera.setPreviewDisplay(holder);
               mCamera.startPreview();

               // Preview callback used whenever new viewfinder frame is available
               mCamera.setPreviewCallback(new PreviewCallback() {
                  public void onPreviewFrame(byte[] data, Camera camera)
                  {
                      if ( (mDrawOnTop == null) || mFinished )
                          return;

                      if (mDrawOnTop.mBitmap == null)
                      {
                          // Initialize the draw-on-top companion
                          Camera.Parameters params = camera.getParameters();
                          mDrawOnTop.mImageWidth = params.getPreviewSize().width;
                          mDrawOnTop.mImageHeight = params.getPreviewSize().height;
                          mDrawOnTop.mBitmap = Bitmap.createBitmap(mDrawOnTop.mImageWidth, 
                          mDrawOnTop.mImageHeight, Bitmap.Config.RGB_565);
                          mDrawOnTop.mRGBData = new int[mDrawOnTop.mImageWidth * mDrawOnTop.mImageHeight]; 
                          mDrawOnTop.mYUVData = new byte[data.length];                    
                      }

                      // Pass YUV data to draw-on-top companion
                      System.arraycopy(data, 0, mDrawOnTop.mYUVData, 0, data.length);
                      mDrawOnTop.invalidate();
                  }
               });
            } 
            catch (IOException exception) {
                mFinished = true;
                mCamera.setPreviewCallback(null);
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            // Surface will be destroyed when we return, so stop the preview.
            // Because the CameraDevice object is not a shared resource, it's very
            // important to release it when the activity is paused.
            mFinished = true;
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;

        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            // Now that the size is known, set up the camera parameters and begin
            // the preview.
            Camera.Parameters parameters = mCamera.getParameters();
            parameters.setPreviewSize(320, 240);
            parameters.setPreviewFrameRate(15);
            parameters.setSceneMode(Camera.Parameters.SCENE_MODE_NIGHT);
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
            mCamera.setParameters(parameters);
            mCamera.startPreview();
        }

    } // end surfaceview
4

2 回答 2

0

尝试先打电话stopPreview(),然后再打电话setPreviewCallback(null)

于 2013-04-08T13:30:47.613 回答
-1

发现有效的答案是将 System.exit(0) 放在 onPause 方法中。解决了所有问题,应用程序可靠且 100% 稳定。退出活动时不再崩溃。

将 finish() 放在 onPause 方法中不起作用,并且在使用表面视图退出活动后,应用程序继续随机崩溃。

有助于减少早期崩溃的一件事是更改 stopPreview() 和 setpreviewCalback(null) 的位置。并删除 setpreviewcallback(null)。在有意退出活动之前调用这些修改会减少崩溃次数。

于 2013-04-09T12:31:52.273 回答