简体   繁体   中英

occur NullPointerException Attempt to invoke virtual method on a null object reference

I try test usb camera preview on raspberry android.

first, my raspberry pi is arm processor.

and usb camera not problem device.

after I checked occur NullPointerException , try null check

Log.d(TAG,"Camera is connected?"+camera);
result is Camera is connected?null

so, I think raspberry pi is it seems unable to detect.

MainActivity.class

public class MainActivity extends Activity implements SurfaceHolder.Callback {
     Camera camera;
     SurfaceView surfaceView;
     SurfaceHolder surfacehHolder;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
     .
     .
     getWindow().setFormat(PixelFormat.UNKNOWN);

     surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
     surfaceHolder = surfaceView.getHolder();
     surfaceHolder.addCallback(this);
     surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
     }

     @Override
     public void surfaceCreated(SurfaceHolder holder) {

     camera = Camera.open();
     camera.stopPreview();
     Camera.Parameters param = camera.getParameters();
     param.setRotation(90);
     camera.setParameters(param);

     try {
         camera.setPreviewDisplay(surfaceHolder);
         camera.startPreview();
     } catch {
        return;
     }
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
       refreshCamera();
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
       camera.stopPreview();
       camera.release();
       camera = null;
  }

  public void refreshCamera() {
      if (surfaceHolder.getSurface() = null) {
         return ;
      } 
      try {
          camera.stopPreview();
      } catch {
      }
  }
 }

this code success work in different device. (not raspberry pi) but only raspberry pi device , occur exception

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.stopPreview()' on a null object reference
at kr.c004245.dom.camera_raspberry_preview.MainActivity.surfaceCreated(MainActivity.java:134)
at android.view.SurfaceView.updateWindow(SurfaceView.java:583)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:177)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2063)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5488)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)

perhaps, raspberry pi android different camera open? if you know. please advice for me

thanks.

The javadocs for Camera say:

This class was deprecated in API level 21.

We recommend using the new android.hardware.camera2 API for new applications.

And the javadoc for open() says:

Creates a new Camera object to access the first back-facing camera on the device. If the device does not have a back-facing camera, this returns null.

So there are a number of possible explanations:

  • Your device does not have a back-facing camera.
  • Your device's back-facing camera is somehow misconfigured at the OS level on your Raspberry Pi ... to the extent that Android can't find it, or can't use it.
  • This deprecated API is not properly implemented on Raspberry Pi Android.

I recommend that you do what the javadoc says, and recode to use the camera2 APIs. Then proceed to debug that .

If you cannot use the camera2 APIs, then you may need to use getNumberOfCameras and getCameraInfo(...) to figure out which camera to use, then call open(ID) to open the selected camera. (Actually, just calling and displaying getNumberOfCameras would be helpful.)

UPDATE

After some more digging, I think the answer is that camera support on an Android Raspberry Pi is pretty rough. For example:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM