如何解决我的代码中显示的错误?

huangapple go评论74阅读模式
英文:

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:

https://github.com/journeyapps/zxing-android-embedded

huangapple
  • 本文由 发表于 2023年6月6日 17:05:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76413035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定