当带有相机预览的片段可见时,setcurrentItem 不起作用

setcurrentItem not working when the fragment with the camera preview is visible

我正在使用 qReader 库,根据我的日志,它按预期工作,与大错误日志相反,除非尝试在 MainFragment 中使用 viewPager.setCurrentItem(0) 更改片段。我希望有人能认识到这些错误,并能告诉我它是否与我更改片段的问题有关。如果没有人认出这些错误,那么我猜它们是特定于图书馆的,我会在图书馆的 github.

上提出一个问题

日志摘要

错误日志

2020-01-22 06:02:06.940 794-29400/? E/ResolverController: No valid NAT64 prefix (118, <unspecified>/0)
2020-01-22 06:02:11.756 838-31771/? E/libc: Access denied finding property "ro.camera.req.fmq.size"
2020-01-22 06:02:11.757 838-31771/? E/libc: Access denied finding property "ro.camera.res.fmq.size"
2020-01-22 06:02:11.804 838-1444/? E/CamX: [ERROR][PPROC  ] camxipenode.cpp:3598 GetEISMargin() No margin for stabilization type 0
2020-01-22 06:02:11.804 838-1444/? E/CamX: [ERROR][PPROC  ] camxipenode.cpp:3598 GetEISMargin() No margin for stabilization type 0
2020-01-22 06:02:11.804 838-1444/? E/CamX: [ERROR][PPROC  ] camxipenode.cpp:3598 GetEISMargin() No margin for stabilization type 0
2020-01-22 06:02:11.859 838-1444/? E/CamX: [ERROR][PPROC  ] camxipenode.cpp:3598 GetEISMargin() No margin for stabilization type 0
2020-01-22 06:02:11.859 838-1444/? E/CamX: [ERROR][PPROC  ] camxipenode.cpp:3598 GetEISMargin() No margin for stabilization type 0
2020-01-22 06:02:11.859 838-1444/? E/CamX: [ERROR][PPROC  ] camxipenode.cpp:3598 GetEISMargin() No margin for stabilization type 0
2020-01-22 06:02:11.859 838-1444/? E/CamX: [ERROR][PPROC  ] camxipenode.cpp:3598 GetEISMargin() No margin for stabilization type 0
2020-01-22 06:02:11.876 838-1444/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1556 SetMetadataByTag() Invalid slot; cannot set metadata tag 80090003
2020-01-22 06:02:11.876 838-1444/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1720 PublishMetadataList() Invalid slot, cannot publish metadata list
2020-01-22 06:02:11.934 838-1444/? E/CamX: [ERROR][SENSOR ] camxsensornode.cpp:2174 LoadPDlibrary() PD library CreateLib failure result=0, m_pPDLib=0x7d44b54ec0
2020-01-22 06:02:11.937 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4702 CreateBufferManagers() invalid type streamConfig 
2020-01-22 06:02:12.042 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4702 CreateBufferManagers() invalid type streamConfig 
2020-01-22 06:02:12.049 838-1444/? E/CamX: [ERROR][STATS_AF] camxcaecstatsprocessor.cpp:3392 ReadTuningModeParameter() PropertyIDUsecaseChiTuningModeParameter is not published!
2020-01-22 06:02:12.052 838-1444/? E/CamX: [ERROR][STATS_AEC] aec_set.cpp:2692: aec_set_param Aec_Error: Sensor Config input is wrong
2020-01-22 06:02:12.052 838-1444/? E/CamX: [ERROR][STATS_AEC] aec_led_calibration.cpp:560: aec_led_cal_apply_calibration Invalid pointer 0x7d3c0ff340 0x0
2020-01-22 06:02:12.052 838-1444/? E/CamX: [ERROR][STATS_AEC] aec_set.cpp:1350: aec_set_fps_range Aec_Error invalid input 417 
2020-01-22 06:02:12.052 838-1444/? E/CamX: [ERROR][STATS_AEC] aec_set.cpp:225: aec_calculate_sensitivity Set sensor configuration has not been set
2020-01-22 06:02:12.052 838-1444/? E/CamX: [ERROR][STATS_AEC] aec_set.cpp:225: aec_calculate_sensitivity Set sensor configuration has not been set
2020-01-22 06:02:12.052 838-1444/? E/CamX: [ERROR][STATS  ] gcamfastaeutil.cpp:1154 SetTuningData() [FastAE] ERROR! Failed to get the tuning data
2020-01-22 06:02:12.052 838-1444/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1397 GetMetadataByTag() Invalid Slot to get a metadata from
2020-01-22 06:02:12.052 838-1444/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1397 GetMetadataByTag() Invalid Slot to get a metadata from
2020-01-22 06:02:12.052 838-1444/? E/CamX: [ERROR][STATS_AEC] aec_led_calibration.cpp:560: aec_led_cal_apply_calibration Invalid pointer 0x7d3c0ff340 0x0
2020-01-22 06:02:12.052 838-1444/? E/CamX: [ERROR][STATS_AEC] aec_set.cpp:1350: aec_set_fps_range Aec_Error invalid input 417 
2020-01-22 06:02:12.052 838-1444/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1397 GetMetadataByTag() Invalid Slot to get a metadata from
2020-01-22 06:02:12.053 838-1444/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1397 GetMetadataByTag() Invalid Slot to get a metadata from
2020-01-22 06:02:12.061 838-1444/? E/GoogTuning: Error! Unsupported tuning mode (usecase: 65535, sensorMode: 65535) - using the default tuning header instead
2020-01-22 06:02:12.077 838-1444/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1397 GetMetadataByTag() Invalid Slot to get a metadata from
2020-01-22 06:02:12.083 838-1439/? E/CamX: [ERROR][STATS_AF] pdlib_lcr_utils.cpp:136 pdaf_lcr_get_vertical_blocks() Cound not find a good partition, set mega_block_y = 1
2020-01-22 06:02:12.116 838-1444/? E/CamX: [ERROR][HAL    ] camxsession.cpp:3537 GetIntraRealtimePipelineId() Less than two realtime pipelines, no intra result
2020-01-22 06:02:12.116 838-1444/? E/CamX: [ERROR][STATS_AF] camxcafioutil.cpp:378 ReadTuningModeParameter() PropertyIDUsecaseChiTuningModeParameter is not published!
2020-01-22 06:02:12.118 838-1444/? E/GoogTuning: Error! Unsupported tuning mode (usecase: 65535, sensorMode: 65535) - using the default tuning header instead
2020-01-22 06:02:12.118 838-1444/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1397 GetMetadataByTag() Invalid Slot to get a metadata from
2020-01-22 06:02:12.265 838-1444/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1397 GetMetadataByTag() Invalid Slot to get a metadata from
2020-01-22 06:02:12.280 838-1444/? E/CamX: [ERROR][ISP    ] camxnode.cpp:2606 GetSensorModeData() currentMode beyond the MaxSensorModes : 65539
2020-01-22 06:02:12.280 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4686 CreateBufferManagers() no StreamConfig 
2020-01-22 06:02:12.280 838-1444/? E/CamX: [ERROR][ISP    ] camxnode.cpp:2606 GetSensorModeData() currentMode beyond the MaxSensorModes : 65539
2020-01-22 06:02:12.280 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4686 CreateBufferManagers() no StreamConfig 
2020-01-22 06:02:12.292 838-1444/? E/CamX: [ERROR][ISP    ] camxnode.cpp:2606 GetSensorModeData() currentMode beyond the MaxSensorModes : 65539
2020-01-22 06:02:12.292 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4686 CreateBufferManagers() no StreamConfig 
2020-01-22 06:02:12.293 838-1444/? E/CamX: [ERROR][ISP    ] camxnode.cpp:2606 GetSensorModeData() currentMode beyond the MaxSensorModes : 65539
2020-01-22 06:02:12.293 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4686 CreateBufferManagers() no StreamConfig 
2020-01-22 06:02:12.306 838-1444/? E/CamX: [ERROR][ISP    ] camxnode.cpp:2606 GetSensorModeData() currentMode beyond the MaxSensorModes : 65539
2020-01-22 06:02:12.306 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4686 CreateBufferManagers() no StreamConfig 
2020-01-22 06:02:12.309 838-1444/? E/CamX: [ERROR][ISP    ] camxnode.cpp:2606 GetSensorModeData() currentMode beyond the MaxSensorModes : 65539
2020-01-22 06:02:12.309 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4686 CreateBufferManagers() no StreamConfig 
2020-01-22 06:02:12.309 838-1444/? E/CamX: [ERROR][ISP    ] camxnode.cpp:2606 GetSensorModeData() currentMode beyond the MaxSensorModes : 65539
2020-01-22 06:02:12.309 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4686 CreateBufferManagers() no StreamConfig 
2020-01-22 06:02:12.311 838-1444/? E/CamX: [ERROR][ISP    ] camxnode.cpp:2606 GetSensorModeData() currentMode beyond the MaxSensorModes : 65539
2020-01-22 06:02:12.311 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4686 CreateBufferManagers() no StreamConfig 
2020-01-22 06:02:12.311 838-1444/? E/CamX: [ERROR][ISP    ] camxnode.cpp:2606 GetSensorModeData() currentMode beyond the MaxSensorModes : 65539
2020-01-22 06:02:12.311 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4686 CreateBufferManagers() no StreamConfig 
2020-01-22 06:02:12.311 838-1444/? E/CamX: [ERROR][ISP    ] camxnode.cpp:2606 GetSensorModeData() currentMode beyond the MaxSensorModes : 65539
2020-01-22 06:02:12.311 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4686 CreateBufferManagers() no StreamConfig 
2020-01-22 06:02:12.347 838-1444/? E/CamX: [ERROR][ISP    ] camxnode.cpp:2606 GetSensorModeData() currentMode beyond the MaxSensorModes : 65539
2020-01-22 06:02:12.347 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4686 CreateBufferManagers() no StreamConfig 
2020-01-22 06:02:12.350 838-1444/? E/CamX: [ERROR][ISP    ] camxnode.cpp:2606 GetSensorModeData() currentMode beyond the MaxSensorModes : 65539
2020-01-22 06:02:12.351 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4686 CreateBufferManagers() no StreamConfig 
2020-01-22 06:02:12.352 838-1444/? E/CamX: [ERROR][ISP    ] camxnode.cpp:2606 GetSensorModeData() currentMode beyond the MaxSensorModes : 65539
2020-01-22 06:02:12.352 838-1444/? E/CamX: [ERROR][CORE   ] camxnode.cpp:4686 CreateBufferManagers() no StreamConfig 
2020-01-22 06:02:12.433 838-1444/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1397 GetMetadataByTag() Invalid Slot to get a metadata from
2020-01-22 06:02:12.433 838-1444/? E/CamX: [ERROR][PPROC  ] camxbpsnode.cpp:792 GetSensorModeData() Mode index vendor tag not provided! Using default 0!
2020-01-22 06:02:12.433 838-1444/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1397 GetMetadataByTag() Invalid Slot to get a metadata from
2020-01-22 06:02:12.433 838-1444/? E/CamX: [ERROR][PPROC  ] camxbpsnode.cpp:792 GetSensorModeData() Mode index vendor tag not provided! Using default 0!
2020-01-22 06:02:12.536 838-1089/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1556 SetMetadataByTag() Invalid slot; cannot set metadata tag 80210000
2020-01-22 06:02:12.536 838-1089/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1720 PublishMetadataList() Invalid slot, cannot publish metadata list
2020-01-22 06:02:12.569 838-1089/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1556 SetMetadataByTag() Invalid slot; cannot set metadata tag 80210000
2020-01-22 06:02:12.569 838-1089/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1720 PublishMetadataList() Invalid slot, cannot publish metadata list
2020-01-22 06:02:12.602 838-1089/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1556 SetMetadataByTag() Invalid slot; cannot set metadata tag 80210000
2020-01-22 06:02:12.603 838-1089/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1720 PublishMetadataList() Invalid slot, cannot publish metadata list
2020-01-22 06:02:12.613 838-1439/? E/CamX: [ERROR][STATS_AF] camxcafioutil.cpp:988 ReadAECInput() PropertyIDAECInternal null data
2020-01-22 06:02:12.613 838-1439/? E/CamX: [ERROR][STATS_AF] camxcafioutil.cpp:1015 ReadAECInput() PropertyIDAECFrameInfo null data
2020-01-22 06:02:12.614 838-1436/? E/CamX: [ERROR][STATS  ] camxcaecstatsprocessor.cpp:3347 ProcessRequestFastAE() [FastAE] Failed to apply gain to the stats!
2020-01-22 06:02:12.614 838-1436/? E/CamX: [ERROR][STATS_AEC] aec_process.cpp:1229: aec_process_stats_parsing aec is null or invalid
2020-01-22 06:02:12.614 838-1436/? E/CamX: [ERROR][STATS_AEC] aec_process.cpp:7983: aec_process_preview_and_video Error: invalid stats
2020-01-22 06:02:12.623 838-1436/? E/CamX: [ERROR][STATS  ] camxcaecstatsprocessor.cpp:3347 ProcessRequestFastAE() [FastAE] Failed to apply gain to the stats!
2020-01-22 06:02:12.623 838-1436/? E/CamX: [ERROR][STATS_AEC] aec_process.cpp:1229: aec_process_stats_parsing aec is null or invalid
2020-01-22 06:02:12.625 838-1436/? E/CamX: [ERROR][STATS_AEC] aec_process.cpp:7983: aec_process_preview_and_video Error: invalid stats
2020-01-22 06:02:12.635 838-1438/? E/CamX: [ERROR][STATS_AEC] camxcaecstatsprocessor.cpp:1745 SetAlgoBayerHistValue() Unsupported bayer hist channel!
2020-01-22 06:02:12.635 838-1438/? E/CamX: [ERROR][STATS  ] camxcaecstatsprocessor.cpp:3347 ProcessRequestFastAE() [FastAE] Failed to apply gain to the stats!
2020-01-22 06:02:12.636 838-1089/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1556 SetMetadataByTag() Invalid slot; cannot set metadata tag 80210000
2020-01-22 06:02:12.636 838-1089/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1720 PublishMetadataList() Invalid slot, cannot publish metadata list
2020-01-22 06:02:12.636 838-1438/? E/CamX: [ERROR][STATS_AEC] aec_process.cpp:1229: aec_process_stats_parsing aec is null or invalid
2020-01-22 06:02:12.637 838-1438/? E/CamX: [ERROR][STATS_AEC] aec_process.cpp:7983: aec_process_preview_and_video Error: invalid stats
2020-01-22 06:02:12.644 838-1438/? E/CamX: [ERROR][STATS  ] camxcaecstatsprocessor.cpp:3347 ProcessRequestFastAE() [FastAE] Failed to apply gain to the stats!
2020-01-22 06:02:12.644 838-1438/? E/CamX: [ERROR][STATS_AEC] aec_process.cpp:1229: aec_process_stats_parsing aec is null or invalid
2020-01-22 06:02:12.644 838-1438/? E/CamX: [ERROR][STATS_AEC] aec_process.cpp:7983: aec_process_preview_and_video Error: invalid stats
2020-01-22 06:02:12.669 838-1089/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1556 SetMetadataByTag() Invalid slot; cannot set metadata tag 80210000
2020-01-22 06:02:12.669 838-1089/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1720 PublishMetadataList() Invalid slot, cannot publish metadata list
2020-01-22 06:02:12.737 838-1440/? E/CamX: [ERROR][CORE   ] camxnode.cpp:2267 GetDataCountFromPipeline() Attempting to get metadata count for tag a0550005 when unpublished
2020-01-22 06:02:12.741 838-1437/? E/CamX: [ERROR][STATS_AFD] afd_process.cpp:1533: afd_process_algo_run stats=0x7d4c4d80f8, bg_stats=0x7d4c4d8338, rowSumTime=0.000000
2020-01-22 06:02:12.770 838-1089/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1556 SetMetadataByTag() Invalid slot; cannot set metadata tag 80210000
2020-01-22 06:02:12.770 838-1089/? E/CamX: [ERROR][HAL    ] camxmetadatapool.cpp:1720 PublishMetadataList() Invalid slot, cannot publish metadata list

MainFragment

public class MainFragment extends Fragment implements MainFragmentInterface{
    private Context context;
    View view;
    protected static NonSwipeableViewPager viewPager;
    FragmentPagerAdapter pagerAdapter;

    public MainFragment(Context context) {
        this.context = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main1, container, false);
        viewPager = view.findViewById(R.id.viewPager);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        pagerAdapter = new Main1FragmentPagerAdapter(getChildFragmentManager(), 3, context, this);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(1);
    }

    //MainFragmentInterface
    @Override
    public void onQRCodeScanned(String data) {
        viewPager.setCurrentItem(0);
        Log.d("QRData", "onQRCodeScanned() called")
    }
}

FragmentPagerAdapter

public class Main1FragmentPagerAdapter extends FragmentPagerAdapter{
    Context context;
    int numOfFragments;
    SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
    MainFragmentInterface mainFragmentInterface;

    public Main1FragmentPagerAdapter(FragmentManager fm, int tabCount, Context context, MainFragmentInterface MainFragmentInterface) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        numOfFragments= tabCount;
        this.context = context;
        this.MainFragmentInterface = MainFragmentInterface;
    }


    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                Log.d("QRData", "getItem(0) called");
                return new SubFragment1(context, MainFragmentInterface);
            case 1:
                return new SubFragment2(context, MainFragmentInterface);
            case 2:
                return new SubFragment3(context, MainFragmentInterface);
            default:
                return null;
        }
    }
    @Override
    public int getCount() {
        return numOfFragments;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {//on each instantiation we are saving the reference of the instantiated Fragment in an array, helping us retrieve the Fragment by position
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {//on each destruction we are removing the reference of the destroyed fragment from the array
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    public Fragment getRegisteredFragment(int position) {//get fragment by position in the registeredFragments array
        return registeredFragments.get(position);
    }
}

SubFragment2(QReader)

public class SubFragment2 extends Fragment {

    private Context context;
    private MainFragmentInterface mainFragmentInterface;
    View view;

    private SurfaceView surfaceView;
    private QREader qrEader;
    private FrameLayout frameLayout;

    public SubFragment2(Context context, MainFragmentInterface mainFragmentInterface) {
        this.context = context;
        this.mainFragmentInterface = mainFragmentInterface;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_sub1, container, false);
        frameLayout = view.findViewById(R.id.frameLayout);
        return view;
    }


    public void returnData(String data) {
        Log.d("QRData", data);
        frameLayout.removeAllViews();
        mainFragmentInterface.onQRCodeScanned(data);
    }

    @Override
    public void onResume() {
        super.onResume();
        frameLayout.removeAllViews();
        surfaceView = new SurfaceView(context);
        frameLayout.addView(surfaceView);
        qrEader = new QREader.Builder(context, surfaceView, new QRDataListener() {
            @Override
            public void onDetected(final String data) {
                Log.d("QREader", "Value : " + data);
                returnData(data);
            }
        }).facing(QREader.BACK_CAM)
                .enableAutofocus(true)
                .height(surfaceView.getHeight())
                .width(surfaceView.getWidth())
                .build();
        // Init and Start with SurfaceView
        // -------------------------------
        qrEader.initAndStart(surfaceView);
        if (!qrEader.isCameraRunning()) {
            qrEader.start();
        }
    }

    @Override
    public void onPause() {
        super.onPause();

        // Cleanup in onPause()
        // --------------------
        if(qrEader.isCameraRunning()){
            qrEader.stop();
        }
        qrEader.releaseAndCleanup();
    }
}

It's not necessary to call CameraSource.stop() from the UI thread, but due to an implementation detail it should not be called from the thread that executes receiveDetections. The stop() code waits for this thread to finish, so calling it this way would create a deadlock.

我必须 release/stop 来自 UI 线程的 CameraSource 对象以避免上述死锁。

activity.runOnUiThread(new Runnable() {
          @Override
          public void run() {
            cameraSource.stop();
            cameraRunning = false;
          }
        });