java - ZXING Android嵌入式IntentIntegrator的使用

标签 java android android-intent android-studio zxing

我是 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/

相关文章:

java - Spring Boot 开发者工具自动重启在 IntelliJ 中不起作用

android - 安装后首次运行运行

android - 在 Android 上读取系统文件

android - onActivityResult DialogFragment 不起作用

Android:将附加功能更改为 Intent

java - Navigation Drawer - syncState() 做什么以及为什么应该在 onPostCreate() 中调用它?

java - @MustOverride 注释?

android - 两个或多个 AutoCompleteTextView 不工作

android eclipse 使用地址而不是纬度和经度来启动 map 的 Intent

java - 使用 Java 和 XML 模板 (JDOM) 处理固定宽度文件