インテントを使用してアクティビティを離れて別のアクティビティに移動すると、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