如何在片段中调用 setContentView

How to call setContentView in a fragment

我知道以前有人问过类似的问题,但我已经搜索过但找不到解决方案。

我正在尝试按照说明设置条形码扫描仪here

public class MainFragment extends Fragment implements View.OnClickListener{

    private ZXingScannerView scannerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_warrant, container, false);

        ImageButton scanBar = (ImageButton) view.findViewById(R.id.scan_barcode);
        scanBar.setOnClickListener(this);

        Button checkImei = (Button) view.findViewById(R.id.btn_check);
        checkImei.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.scan_barcode:
                scannerView = new ZXingScannerView(getActivity());
                scannerView.setResultHandler(new ZXingScannerResultHandler());
//                setContentView(scannerView);
                scannerView.startCamera();
                break;
            case R.id.btn_check:
                Toast.makeText(getActivity(), "Button", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    class ZXingScannerResultHandler implements ZXingScannerView.ResultHandler {

        @Override
        public void handleResult(Result result) {
            String resultCode = result.getText();
            Toast.makeText(getActivity(), resultCode, Toast.LENGTH_LONG).show();
//            setContentView(R.layout.activity_start);
            scannerView.stopCamera();
        }
    }
}

在 Activity 中有效,但在片段中移动它时,setContentView 出现错误。是否有 setContentView 的替代方法或使用片段的不同库?

这是 link 的,
Official documentation

关于如何将 ZXingScanner 实现到 Fragment 中

干杯!