我正在尝试使用带有SurfaceView的Android相机,因此用户可以预览相机。我在StackOverflow上学习了各种教程,最后得到了谷歌自己的教程,但我的应用程序崩溃了。这是我的代码。我正在使用Eclipse的Android虚拟设备,模拟相机(我认为不会影响任何东西)和100mb sd卡空间分配。
有两个类,一个是CameraPreview对象,另一个是CameraActivity。
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;
private final String TAG = "TAG";
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 {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
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 e){
// ignore: tried to stop a non-existent preview
}
// 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 e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
public class CameraActivity extends Activity{
private Camera mCamera;
private CameraPreview mPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
}
06-04 14:28:07.726:W / dalvikvm(6115):threadid = 1:线程退出时未捕获异常(组= 0x40a71930) 06-04 14:28:07.956:E / AndroidRuntime(6115):致命异常:主要 06-04 14:28:07.956:E / AndroidRuntime(6115):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.mobilequote / com.example.mobilequote.CameraActivity}:java.lang.NullPointerException 06-04 14:28:07.956:E / AndroidRuntime(6115):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 06-04 14:28:07.956:E / AndroidRuntime(6115):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 06-04 14:28:07.956:E / AndroidRuntime(6115):在android.app.ActivityThread.access $ 600(ActivityThread.java:141) 06-04 14:28:07.956:E / AndroidRuntime(6115):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234) 06-04 14:28:07.956:E / AndroidRuntime(6115):在android.os.Handler.dispatchMessage(Handler.java:99) 06-04 14:28:07.956:E / AndroidRuntime(6115):在android.os.Looper.loop(Looper.java:137) 06-04 14:28:07.956:E / AndroidRuntime(6115):在android.app.ActivityThread.main(ActivityThread.java:5041) 06-04 14:28:07.956:E / AndroidRuntime(6115):at java.lang.reflect.Method.invokeNative(Native Method) 06-04 14:28:07.956:E / AndroidRuntime(6115):at java.lang.reflect.Method.invoke(Method.java:511) 06-04 14:28:07.956:E / AndroidRuntime(6115):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 06-04 14:28:07.956:E / AndroidRuntime(6115):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-04 14:28:07.956:E / AndroidRuntime(6115):at dalvik.system.NativeStart.main(Native Method) 06-04 14:28:07.956:E / AndroidRuntime(6115):引起:java.lang.NullPointerException 06-04 14:28:07.956:E / AndroidRuntime(6115):at com.example.mobilequote.CameraActivity.onCreate(CameraActivity.java:24) 06-04 14:28:07.956:E / AndroidRuntime(6115):在android.app.Activity.performCreate(Activity.java:5104) 06-04 14:28:07.956:E / AndroidRuntime(6115):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 06-04 14:28:07.956:E / AndroidRuntime(6115):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 06-04 14:28:07.956:E / AndroidRuntime(6115):... 11 more
CameraActivity.java:24行 preview.addView(mPreview);
答案 0 :(得分:1)
请参阅我对模拟器的评论。但是你的应用程序崩溃的原因是因为你捕获了相机未能打开并返回null的异常。然后在你的CameraPreview类中将这个空值保存到'mCamera',然后在surfaceCreated中取消引用它:
mCamera.setPreviewDisplay(holder);
所以你原来的问题是它崩溃的原因......它崩溃了,因为你没有很好地处理错误条件。