【问题标题】:Unable to call IntentIntegrator class from ZXing?无法从 ZXing 调用 IntentIntegrator 类?
【发布时间】:2019-03-13 18:36:56
【问题描述】:

Android 新手在这里。我最近开始构建一个条码扫描器应用程序,我偶然发现了这个video。但是,当 IntentIntegrator 类出现时,我在观看视频时遇到了问题。即使我已经在 build.gradle(Module:app) 中实现了 ZXing,我的代码似乎也无法导入该类。

这是我的 build.gradle(Module.app) 代码:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.scanner4"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.journeyapps:zxing-android-embedded:3.2.0@arr'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

这是我的 MainActivity.java 代码:

package com.example.scanner4;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button scan_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scan_btn = (Button) findViewById(R.id.scan_btn);
        final Activity activity = this;

        scan_btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                IntentIntegrator integrator = new IntentIntegrator(activity);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
                integrator.setPrompt("Scan");
                integrator.setCameraId(0);
                integrator.setBeepEnabled(false);
                integrator.setBarcodeImageEnabled(false);
                integrator.initiateScan();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
        if(result != null){
            if(result.getContent() != null){
                Toast.makeText(this,"You cancelled the scanning", Toast.LENGTH_LONG).show();
            }
            else{
                Toast.makeText(this,result.getContents(), Toast.LENGTH_LONG).show();
            }
        }
        else{
            super.onActivityResult(requestCode, resultCode, data);
        }


    }
}

我的问题在于线路:

IntentIntegrator integrator = new IntentIntegrator(activity);

非常感谢您在这件事上的帮助,因为我是 Android 的新手。请原谅我缺乏经验,因为我无法为这个简单的问题找到任何解决方案。

【问题讨论】:

    标签: android


    【解决方案1】:

    问题:这行让你的应用无法解析IntentIntegrator

    implementation 'com.journeyapps:zxing-android-embedded:3.2.0@arr'
    

    要在 gradle 中导入 Android 存档库 (aar),您必须使用 @aar 而不是 @arr

    解决方法:将gradle文件中的依赖改为

    implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar' 
    

    【讨论】:

      【解决方案2】:

      我在您的代码中没有看到任何与 ZXing 相关的 import 行,因此可能是原因。您需要添加缺少的imports,最简单的方法是尝试“优化导入”操作 CTRL+ALT+O 自动拥有它更正。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-27
        • 1970-01-01
        • 2014-04-10
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 2021-11-24
        • 2019-06-16
        相关资源
        最近更新 更多