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的全新用户,因此非常感谢您的帮助。请原谅我的经验不足,因为我找不到这个简单问题的解决方案。
答案 0 :(得分: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'