我正在做一个camara项目,其中一个活动捕获的图像被发送到另一个活动。问题是,当我在拍摄按钮,单击该应用程序崩溃,并显示在Camara应用程序崩溃点击
public class MainActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCamera = getCameraInstance();
// Create the Camera preview view
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
final Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Intent intent = new Intent(MainActivity.this, EditActivity.class);
intent.putExtra(EditActivity.EXTRA_BYTES, data);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
};
// Add a listener to the capture button
ImageButton captureButton = (ImageButton) findViewById(R.id.button_capture);
captureButton.setImageDrawable(getResources().getDrawable(R.drawable.cam));
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, mPicture);
}
}
);
}
@Override
protected void onPause() {
super.onPause();
finish();
mPreview.stopPreview();
}
/**
* A safe way to get an instance of the Camera object.
*
* Returns null if camera is unavailable.
*/
private static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
} catch (Exception ignore) {
// Camera is not available (in use or does not exist). Do nothing.
}
return c;
}
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
if (mCamera != null) {
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
}
} catch (IOException ignore) {
// Do nothing
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.stopPreview();
}
}
private void stopPreview() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// Stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception ignore) {
// Tried to stop a non-existent preview. Do nothing.
}
// Set preview size and make any resize, rotate or reformatting changes here
// Start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception ignore) {
// Do nothing
}
}
}
}
请提供堆栈跟踪和确切的异常 – NightFury
“_and在logcat_上显示空值点异常”当在此处提问时,最好_实际显示LogCat和堆栈跟踪。 – JonasCz