使用ZXING库在Android中的QR Code Scanner错误

时间:2017-03-22 09:43:49

标签: android zxing

我有以下代码。每当我按下一个按钮时,结果都会显示在Display类中,但有时它会将我从应用程序中抛出,有时它可以正常工作。我不知道怎么办?

任何人都可以解释为什么会这样吗?

public class MainActivity extends AppCompatActivity {
    Context con = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onScanClick(View view) {
            initiateScan();
    }

    private void initiateScan() {
        IntentIntegrator integrator = new IntentIntegrator((Activity) con);
        integrator.setBeepEnabled(true);
        integrator.initiateScan();
    }

    // Get the results:
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if(result != null) {
            if(result.getContents() == null) {
                Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();

            } else {
                ShowResult.Dispay((Activity) con,result.getContents());
                Vibrator.vibrate(250L,con);
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

0 个答案:

没有答案