surfaceView类崩溃了。如何在没有崩溃的情况下将此活动留给另一个活动?
我使用此意图离开此类并打开另一个名为CameraView类的活动:
Intent intent = new Intent(this, CameraView.class);
startActivity(intent);
来自LogCat的stacktrace:
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
答案 0 :(得分:0)
首先调用stopPreview()
,然后再调用setPreviewCallback(null)
答案 1 :(得分:-1)
发现有效的答案是将System.exit(0)放在onPause方法中。修复了所有问题,应用程序可靠,100%稳定。退出活动时不再崩溃。
在onPause方法中放置finish()不起作用,并且在使用曲面视图退出活动后,应用程序在随机时间内继续崩溃。
有助于减少崩溃的一件事是更改stopPreview()和setpreviewCalback(null)的位置。并删除setpreviewcallback(null)。在以意图退出活动之前调用这些修改时减少了崩溃次数。