Я пытаюсь внедрить модуль в мой сканер штрих-кода приложения, это ссылка https://medium.com/cashify-engineering/barcode-reader-using-google-mobile-vision-88b3e9f31668
, когда я нажимаю кнопку, чтобы реализовать это, это выдает мне ошибку ClassCastException: невозможно привести к android .content.Context
, пожалуйста, помогите
package com.example.shopkeeperapp;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.vision.barcode.Barcode;
import com.notbytes.barcode_reader.BarcodeReaderActivity;
public class test extends AppCompatActivity {
private static final int BARCODE_READER_ACTIVITY_REQUEST =Activity.RESULT_OK ;
Button bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
bar = findViewById(R.id.barcode);
bar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent launchIntent = BarcodeReaderActivity.getLaunchIntent(this, true, false);
startActivityForResult(launchIntent, BARCODE_READER_ACTIVITY_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
Toast.makeText(this, "error in scanning", Toast.LENGTH_SHORT).show();
return;
}
if (requestCode == BARCODE_READER_ACTIVITY_REQUEST && data != null) {
Barcode barcode = data.getParcelableExtra(BarcodeReaderActivity.KEY_CAPTURED_BARCODE);
Toast.makeText(this, barcode.rawValue, Toast.LENGTH_SHORT).show();
}
}
}
и ошибка в logcat:
2020-04-30 12:27:41.001 20201-20201/com.example.shopkeeperapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shopkeeperapp, PID: 20201
java.lang.ClassCastException: com.example.shopkeeperapp.test$1 cannot be cast to android.content.Context
at com.notbytes.barcode_reader.BarcodeReaderActivity.getLaunchIntent(BarcodeReaderActivity.java:51)
at com.example.shopkeeperapp.test$1.onClick(test.java:31)
at android.view.View.performClick(View.java:7201)
at android.view.View.performClickInternal(View.java:7170)
at android.view.View.access$3500(View.java:806)
at android.view.View$PerformClick.run(View.java:27562)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7643)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
BarcodeReaderActivity:
public static Intent getLaunchIntent(View.OnClickListener context, boolean autoFocus, boolean useFlash) {
Intent intent = new Intent((Context) context, BarcodeReaderActivity.class);
intent.putExtra(KEY_AUTO_FOCUS, autoFocus);
intent.putExtra(KEY_USE_FLASH, useFlash);
return intent;
}