我是 Android 开发新手,仍在尝试掌握一些概念。我发现最好的学习方法就是直接进入一个项目的深处。话虽如此,这是我的问题:
我已将 ZXing Android Embedded 集成到我的应用程序中;但是,我无法理解您使用 IntentIntegrator 的方式。我现在想做的就是当用户点击按钮时将 QR 扫描仪调用到屏幕上。我一直在尝试按照他们的 github 链接 [此处][1] 上的说明进行操作,但没有成功。
到目前为止,我的函数如下所示:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
IntentIntegrator integrator = new IntentIntegrator(this);
IntentIntegrator.forFragment(this).initiateScan();
}
});
我不断收到错误消息:
Error:(109, 25) error: constructor IntentIntegrator in class IntentIntegrator cannot be applied to given types; required: Activity found: Intent reason: actual argument Intent cannot be converted to Activity by method invocation conversion
此外,当我将鼠标放在 Android Studio 中的“(this)”上时,它会显示:
anonymous android.view.View.onClickListener
任何帮助将不胜感激,谢谢!如果您需要任何其他信息,请告诉我。
最佳答案
比 ChrisStillwell 更简单的解决方案是让您的 Activity/fragment 类实现 OnClickListener
,这样您就不需要引用变量:
public class SomeFragment extends Fragment implements View.OnClickListener {
// Rest of your code
@Override
public void onClick(View v) {
if (v.getId == button.getId) {
IntentIntegrator integrator = new IntentIntegrator(getActivity());
IntentIntegrator.forFragment(this).initiateScan();
}
}
}
如果您要实现 fragment 类,请注意,在创建 IntentIntegrator
时必须调用 getActivity()
。
关于java - ZXING Android嵌入式IntentIntegrator的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32852754/