私はAndroidの例を使って練習していますが、独自のカメラアプリケーションを作成する場合は、カメラの画像を表示する独自のサーフェスビューを作成すると、エラーjava.lang.NullPointerExceptionが発生します。
これが私のコードです:
package com.example.prueba;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "CameraPreview";
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 {
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());
}
}
}
//And here the error log:
//11-28 13:19:24.451: E/AndroidRuntime(870): FATAL EXCEPTION: main
//11-28 13:19:24.451: E/AndroidRuntime(870): java.lang.NullPointerException
//11-28 13:19:24.451: E/AndroidRuntime(870): at com.example.prueba.CameraPreview.surfaceCreated(CameraPreview.java:34)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.view.SurfaceView.access$000(SurfaceView.java:81)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1820)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.view.Choreographer.doFrame(Choreographer.java:525)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.os.Handler.handleCallback(Handler.java:615)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.os.Handler.dispatchMessage(Handler.java:92)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.os.Looper.loop(Looper.java:137)
//11-28 13:19:24.451: E/AndroidRuntime(870): at android.app.ActivityThread.main(ActivityThread.java:4745)
//11-28 13:19:24.451: E/AndroidRuntime(870): at java.lang.reflect.Method.invokeNative(Native Method)
//11-28 13:19:24.451: E/AndroidRuntime(870): at java.lang.reflect.Method.invoke(Method.java:511)
//11-28 13:19:24.451: E/AndroidRuntime(870): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
//11-28 13:19:24.451: E/AndroidRuntime(870): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
//11-28 13:19:24.451: E/AndroidRuntime(870): at dalvik.system.NativeStart.main(Native Method)