英文:
How Can I solve the errors shown in my code
问题
以下是已翻译的代码部分:
The Errors Shown include the following:
以下是显示的错误信息:
Cannot resolve symbol 'me' in line 17
第17行无法解析符号 'me'
Cannot resolve symbol 'ZXingScannerView' in line 19
第19行无法解析符号 'ZXingScannerView'
Cannot resolve method 'setContentView(ZXingScannerView)' in line 40
第40行无法解析方法 'setContentView(ZXingScannerView)'
Cannot resolve method 'setResultHandler(ScanQRActivity)'
无法解析方法 'setResultHandler(ScanQRActivity)'
Method does not override method from its superclass in line 45
第45行的方法未覆盖其超类中的方法
Overriding method should call `super.onRequestPermissionsResult` in line 66
覆盖的方法应在第66行调用 `super.onRequestPermissionsResult`
Cannot resolve method 'startCamera()' in line 42
第42行无法解析方法 'startCamera()'
package com.gretsa.studentattendance;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ScanQRActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final int CAMERA_PERMISSION_REQUEST_CODE = 200;
private static final int SCAN_QR_REQUEST_CODE = 201;
private ZXingScannerView scannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanqr);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
startQRScanner();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
}
}
private void startQRScanner() {
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
scannerView.setResultHandler(this);
scannerView.startCamera();
}
@Override
public void handleResult(Result result) {
String qrCodeData = result.getText();
Toast.makeText(this, "Scanned: " + qrCodeData, Toast.LENGTH_SHORT).show();
// Pass the scanned QR code data back to the calling activity
Intent intent = new Intent();
intent.putExtra("qr_code_data", qrCodeData);
setResult(RESULT_OK, intent);
finish();
}
@Override
protected void onPause() {
super.onPause();
if (scannerView != null) {
scannerView.stopCamera();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startQRScanner();
} else {
Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show();
finish();
}
}
}
}
请注意,我已将HTML实体编码转换为相应的文本字符以进行翻译。如果需要更多帮助或其他翻译,请告诉我。
英文:
The Errors Shown include the following:
Cannot resolve symbol 'me' in line 17
Cannot resolve symbol 'ZXingScannerView' in line 19
Cannot resolve method 'setContentView(ZXingScannerView)' in line 40
Cannot resolve method 'setResultHandler(ScanQRActivity)'
Method does not override method from its superclass in line 45
Overriding method should call super.onRequestPermissionsResult
in line 66
Cannot resolve method 'startCamera()' in line 42
package com.gretsa.studentattendance;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ScanQRActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final int CAMERA_PERMISSION_REQUEST_CODE = 200;
private static final int SCAN_QR_REQUEST_CODE = 201;
private ZXingScannerView scannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanqr);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
startQRScanner();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
}
}
private void startQRScanner() {
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
scannerView.setResultHandler(this);
scannerView.startCamera();
}
@Override
public void handleResult(Result result) {
String qrCodeData = result.getText();
Toast.makeText(this, "Scanned: " + qrCodeData, Toast.LENGTH_SHORT).show();
// Pass the scanned QR code data back to the calling activity
Intent intent = new Intent();
intent.putExtra("qr_code_data", qrCodeData);
setResult(RESULT_OK, intent);
finish();
}
@Override
protected void onPause() {
super.onPause();
if (scannerView != null) {
scannerView.stopCamera();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startQRScanner();
} else {
Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show();
finish();
}
}
}
}
Tried analysing the problem but that didnt help
答案1
得分: 1
看起来您尚未添加zxing依赖项,请尝试在您的gradle文件中添加以下内容:
allprojects {
repositories {
jcenter()
}
}
dependencies {
implementation 'com.google.zxing:core:3.3.0'
}
如果这不起作用,请尝试使用这个库:
https://github.com/journeyapps/zxing-android-embedded
英文:
It looks like you haven't added zxing dependencies, try adding these in your gradle file:
allprojects {
repositories {
jcenter()
}
}
dependencies {
implementation 'com.google.zxing:core:3.3.0'
}
if it doesn't work, try this library:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论