ユーザーがカメラをプレビューできるように、SurfaceViewでAndroidのカメラを使用しようとしています。StackOverflow でいろいろなチュートリアルをたどり、最終的に Google 独自のチュートリアルにたどり着いたのですが、アプリがクラッシュしてしまいました。これが私のコードです。私はEclipseのAndroid仮想デバイス、エミュレートされたカメラ(何にも影響しないと思います)、および割り当てられた100MBのSDカードスペースを使用しています。
CameraPreview オブジェクトと CameraActivity の 2 つのクラスがあります。
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: キャッチされない例外で終了するスレッド (group=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): java.lang.reflect.Method.invokeNative(Native Method) 06-04 14:28:07.956: E/AndroidRuntime(6115): java.lang.reflect.Method. (Method.java:511) 06-04 14:28:07.956: E/AndroidRuntime(6115): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-04 14: 28:07.956: E/AndroidRuntime(6115): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-04 14:28:07.956: E/AndroidRuntime(6115): 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 もっと見る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 件以上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 件以上
CameraActivity.java:24 の行は preview.addView(mPreview); です。