You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Camera2开发出现cameraId was null崩溃,请求技术排查帮助

Fix for Camera2 Framework cameraId Null Crash

Hey there, let's dive into why you're hitting this cameraId was null crash and fix it up!

Root Cause

Looking at your setupCamera() method, the very first line is:

if(mCameraDevice == null) return;

This is the critical mistake! When setupCamera() gets called from onSurfaceTextureAvailable(), your mCameraDevice hasn't been initialized yet (you only set it in the onOpened() callback later). So this line immediately exits the method before you ever run the code that finds and assigns a valid camera ID to mCameraId. That's why when connectCamera() tries to call cameraManager.openCamera(mCameraId, ...), mCameraId is still null.

Step-by-Step Fix

  1. Remove the invalid check from setupCamera
    Delete that first line if(mCameraDevice == null) return; entirely. Your setupCamera's job is to prepare camera parameters (like getting the right camera ID) before opening the camera, so checking for an already opened camera here makes no sense.

    Here's the corrected setupCamera() method start:

    private void setupCamera(int width, int height) {
        CameraManager cameraManager = (CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE);
        try {
            if (cameraManager != null) {
                // Rest of your existing logic here...
    
  2. Add Permission Result Handling (Bonus Fix)
    You'll also notice that after requesting camera permission on Android 6.0+, you don't have an onRequestPermissionsResult() override. This means if the user grants permission after being prompted, your app won't automatically connect to the camera. Add this to your fragment:

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CAMERA_PERMISSION_RESULT) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                connectCamera();
            } else {
                Toast.makeText(getActivity(), "Camera permission is required to use this feature", Toast.LENGTH_LONG).show();
            }
        }
    }
    
  3. Safety Check for Camera List (Optional but Recommended)
    To avoid edge cases where no cameras are available, add a check after getting the camera ID list:

    if (cameraManager != null) {
        String[] cameraIds = cameraManager.getCameraIdList();
        if (cameraIds.length == 0) {
            Toast.makeText(getActivity(), "No cameras available", Toast.LENGTH_LONG).show();
            return;
        }
        // Rest of your logic...
    }
    

Why This Works

By removing that incorrect check, your code will now run through the camera characteristics lookup, find the back camera, assign a valid mCameraId, and then connectCamera() will have the non-null ID it needs to open the camera successfully.

内容的提问来源于stack exchange,提问作者PadN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:49:31