我想在Fragment中实现QRcode扫描程序,但setContentView显示错误

时间:2018-01-17 12:17:12

标签: android android-layout android-fragments fragment android-fragmentactivity

public class EarnFragment extends Fragment {

    private Button scan;
    private ZXingScannerView scannerview;

    public EarnFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(fragment_earn, container, false);

        scan = (Button)v.findViewById(R.id.scan);
        scan.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                scannerview=new ZXingScannerView(getActivity());
                setContentView(scannerview);
                scannerview.startCamera();
                scannerview.setResultHandler(new ZXingScannerView.ResultHandler() {
                    @Override
                    public void handleResult(Result result) {
                        String resultcode=result.getText();
                        Toast.makeText(getActivity(),resultcode, Toast.LENGTH_SHORT).show();
                    }
                });

               // Toast.makeText(getActivity(),"Ok",Toast.LENGTH_SHORT).show();
            }
        });

        return  v;
    }
}

SetContentView Fragments无效,为什么?什么是Replacement的{​​{1}}?

1 个答案:

答案 0 :(得分:0)

你可以这样做

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v= inflater.inflate(fragment_earn, container, false);

    IntentIntegrator integrator = new IntentIntegrator(getActivity());
    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
    integrator.setPrompt("Scan a QRCode");
    integrator.setCameraId(0);  // Use a specific camera of the device
    integrator.setBeepEnabled(false);
    integrator.setBarcodeImageEnabled(true);

    scan = (Button)v.findViewById(R.id.scan);
    scan.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            integrator.initiateScan();

        }
    });

并添加onActivityResult()

protected void onActivityResult(final int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        if (resultCode != 3 && resultCode != 0 && result.getContents() != null) {      
            new Thread(new Runnable() {
                public void run() {
                    // do something
                          }

            }).start();
          }

        else if (result.getContents() == null) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        }

}
}

并添加此依赖项

compile 'com.journeyapps:zxing-android-embedded:3.4.0'