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

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

How Can I solve the errors shown in my code

问题

以下是已翻译的代码部分:

  1. The Errors Shown include the following:
  2. 以下是显示的错误信息
  3. Cannot resolve symbol 'me' in line 17
  4. 17行无法解析符号 'me'
  5. Cannot resolve symbol 'ZXingScannerView' in line 19
  6. 19行无法解析符号 'ZXingScannerView'
  7. Cannot resolve method 'setContentView(ZXingScannerView)' in line 40
  8. 40行无法解析方法 'setContentView(ZXingScannerView)'
  9. Cannot resolve method 'setResultHandler(ScanQRActivity)'
  10. 无法解析方法 'setResultHandler(ScanQRActivity)'
  11. Method does not override method from its superclass in line 45
  12. 45行的方法未覆盖其超类中的方法
  13. Overriding method should call `super.onRequestPermissionsResult` in line 66
  14. 覆盖的方法应在第66行调用 `super.onRequestPermissionsResult`
  15. Cannot resolve method 'startCamera()' in line 42
  16. 42行无法解析方法 'startCamera()'
  17. package com.gretsa.studentattendance;
  18. import android.Manifest;
  19. import android.content.Intent;
  20. import android.content.pm.PackageManager;
  21. import android.os.Bundle;
  22. import android.view.View;
  23. import android.widget.Toast;
  24. import androidx.annotation.NonNull;
  25. import androidx.appcompat.app.AppCompatActivity;
  26. import androidx.core.app.ActivityCompat;
  27. import androidx.core.content.ContextCompat;
  28. import com.google.zxing.Result;
  29. import me.dm7.barcodescanner.zxing.ZXingScannerView;
  30. public class ScanQRActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
  31. private static final int CAMERA_PERMISSION_REQUEST_CODE = 200;
  32. private static final int SCAN_QR_REQUEST_CODE = 201;
  33. private ZXingScannerView scannerView;
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_scanqr);
  38. if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
  39. startQRScanner();
  40. } else {
  41. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
  42. }
  43. }
  44. private void startQRScanner() {
  45. scannerView = new ZXingScannerView(this);
  46. setContentView(scannerView);
  47. scannerView.setResultHandler(this);
  48. scannerView.startCamera();
  49. }
  50. @Override
  51. public void handleResult(Result result) {
  52. String qrCodeData = result.getText();
  53. Toast.makeText(this, "Scanned: " + qrCodeData, Toast.LENGTH_SHORT).show();
  54. // Pass the scanned QR code data back to the calling activity
  55. Intent intent = new Intent();
  56. intent.putExtra("qr_code_data", qrCodeData);
  57. setResult(RESULT_OK, intent);
  58. finish();
  59. }
  60. @Override
  61. protected void onPause() {
  62. super.onPause();
  63. if (scannerView != null) {
  64. scannerView.stopCamera();
  65. }
  66. }
  67. @Override
  68. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  69. if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
  70. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  71. startQRScanner();
  72. } else {
  73. Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show();
  74. finish();
  75. }
  76. }
  77. }
  78. }

请注意,我已将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

  1. package com.gretsa.studentattendance;
  2. import android.Manifest;
  3. import android.content.Intent;
  4. import android.content.pm.PackageManager;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Toast;
  8. import androidx.annotation.NonNull;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import androidx.core.app.ActivityCompat;
  11. import androidx.core.content.ContextCompat;
  12. import com.google.zxing.Result;
  13. import me.dm7.barcodescanner.zxing.ZXingScannerView;
  14. public class ScanQRActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
  15. private static final int CAMERA_PERMISSION_REQUEST_CODE = 200;
  16. private static final int SCAN_QR_REQUEST_CODE = 201;
  17. private ZXingScannerView scannerView;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_scanqr);
  22. if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
  23. startQRScanner();
  24. } else {
  25. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
  26. }
  27. }
  28. private void startQRScanner() {
  29. scannerView = new ZXingScannerView(this);
  30. setContentView(scannerView);
  31. scannerView.setResultHandler(this);
  32. scannerView.startCamera();
  33. }
  34. @Override
  35. public void handleResult(Result result) {
  36. String qrCodeData = result.getText();
  37. Toast.makeText(this, "Scanned: " + qrCodeData, Toast.LENGTH_SHORT).show();
  38. // Pass the scanned QR code data back to the calling activity
  39. Intent intent = new Intent();
  40. intent.putExtra("qr_code_data", qrCodeData);
  41. setResult(RESULT_OK, intent);
  42. finish();
  43. }
  44. @Override
  45. protected void onPause() {
  46. super.onPause();
  47. if (scannerView != null) {
  48. scannerView.stopCamera();
  49. }
  50. }
  51. @Override
  52. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  53. if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
  54. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  55. startQRScanner();
  56. } else {
  57. Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show();
  58. finish();
  59. }
  60. }
  61. }
  62. }

Tried analysing the problem but that didnt help

答案1

得分: 1

看起来您尚未添加zxing依赖项,请尝试在您的gradle文件中添加以下内容:

  1. allprojects {
  2. repositories {
  3. jcenter()
  4. }
  5. }
  6. dependencies {
  7. implementation 'com.google.zxing:core:3.3.0'
  8. }

如果这不起作用,请尝试使用这个库:

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

英文:

It looks like you haven't added zxing dependencies, try adding these in your gradle file:

  1. allprojects {
  2. repositories {
  3. jcenter()
  4. }
  5. }
  6. dependencies {
  7. implementation 'com.google.zxing:core:3.3.0'
  8. }

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:

确定