gpt4 book ai didi

java - 获取处理对相机的调用并预览它的 javaNullPointer 异常

转载 作者:行者123 更新时间:2023-11-30 09:19:42 25 4
gpt4 key购买 nike

我正在关注 Android 开发人员的相机指南。然而,当我尝试运行该 Activity 时,我得到了一个 Java lang NullPointer 异常,我不明白它来自哪里。

这是我记下的相机 Activity 。

public class Call extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call);

// Create an instance of Camera
mCamera = getCameraInstance();
System.out.println(Camera.open());

// 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.start_call, menu);
return true;
}

/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
System.out.println("Has a camera it has "+Camera.getNumberOfCameras());
getCameraInstance();
return true;
} else {
// no camera on this device
System.out.println("Does not have a camera");
return false;
}
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
System.out.println("Got the camera");
}
catch (Exception e){
// Camera is not available (in use or does not exist)
System.out.println("Not available");
}
return c; // returns null if camera is unavailable
}

}

这是相机预览类

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
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("error", "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("error", "Error starting camera preview: " + e.getMessage());
}
}
}

这是我得到的错误。

07-24 17:55:11.856: E/AndroidRuntime(796): FATAL EXCEPTION: main
07-24 17:55:11.856: E/AndroidRuntime(796): java.lang.NullPointerException
07-24 17:55:11.856: E/AndroidRuntime(796): at com.reflap.reflap.CameraPreview.surfaceCreated(CameraPreview.java:31)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.SurfaceView.access$000(SurfaceView.java:81)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1820)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.Choreographer.doFrame(Choreographer.java:525)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.os.Handler.handleCallback(Handler.java:615)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.os.Handler.dispatchMessage(Handler.java:92)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.os.Looper.loop(Looper.java:137)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-24 17:55:11.856: E/AndroidRuntime(796): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 17:55:11.856: E/AndroidRuntime(796): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 17:55:11.856: E/AndroidRuntime(796): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-24 17:55:11.856: E/AndroidRuntime(796): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-24 17:55:11.856: E/AndroidRuntime(796): at dalvik.system.NativeStart.main(Native Method)
07-24 17:55:15.045: I/Process(796): Sending signal. PID: 796 SIG: 9
07-24 17:55:15.956: E/Trace(817): error opening trace file: No such file or directory (2)

我不知道 null 是从哪里来的。我确实打印出了这个 System.out.println(Camera.open());它打印出 null,这很奇怪,但错误堆栈指向这个,mCamera.setPreviewDisplay(holder);

我在 android 模拟器上运行它,但我打开相机并在相机应用程序中对其进行了测试。

最佳答案

您没有代码来测试您是否有相机。在使用它做任何其他事情之前,请确保您有相机。

在此代码中,我正在寻找唯一的摄像头或面向背面的摄像头......

public int getCameraId() { 
Log.d(TAG, "getCameraId()");
int cameraId = -1;
// Search for the back facing camera (or any camera)
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_BACK || numberOfCameras == 1) {
Log.d(TAG, "CameraInfo.CAMERA_FACING_BACK = "
+ (info.facing == CameraInfo.CAMERA_FACING_BACK));
cameraId = i;
break;
}
}
return cameraId;
}

public static Camera getCameraInstance(int cameraId) {
Log.d(TAG, "getCameraInstance("+cameraId+")");
Camera c = null;
try {
c = Camera.open(cameraId); // attempt to get a Camera instance
Camera.Parameters cp = c.getParameters();
Log.d(TAG, "getCameraInstance("+cameraId+"): Camera.Parameters = "
+ cp.flatten());
} catch (Exception e) {
Log.d(TAG, "Camera.open("+cameraId+") exception="+e);
}
Log.d(TAG, "getCameraInstance("+cameraId+") = "+c);
return c; // returns null if camera is unavailable
}

在调用 getCameraInstance 之前,调用会检查 getCameraId 返回的 cameraId > -1。

关于java - 获取处理对相机的调用并预览它的 javaNullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17846101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com