0

私は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)
4

1 に答える 1

0

問題は、マニフェストにこれを入れなかったため、mCameraがnullであったことです。

<uses-permission android:name="android.permission.CAMERA" />

これで問題は解決しましたが、表面ビューが黒くなり、カメラ画像が表示されず、カメラエラー100が発生します

カメラエラー100を解決するために、新しいサイズを設定するために行を追加してsurfaceChangedを変更しました。

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
        Parameters parameters= mCamera.getParameters();
        parameters.setPictureSize(w, h);
        mCamera.setParameters(parameters);

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }

これで正しく動作しますが、マニフェストにandroid sdkの例のように画面方向のランドスケープを配置したため、なぜこれらの行を追加する必要があるのか​​わかりません。

<activity android:name=".CameraActivity"
      android:label="@string/app_name"

      android:screenOrientation="landscape">
      <!-- configure this activity to use landscape orientation -->

      <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

于 2012-11-29T09:33:18.697 に答える