0

I'm trying to implement a module into my app barcode scanner this is the link https://medium.com/cashify-engineering/barcode-reader-using-google-mobile-vision-88b3e9f31668

when I'm making a button to implement this its giving me an error of ClassCastException: cannot be cast to android.content.Context

please help

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();
    }

    }

    }

and the error in 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;
}
  • Change Intent intent = new Intent((Context) context, BarcodeReaderActivity.class); to Intent intent = new Intent(this, BarcodeReaderActivity.class); – MMG Apr 30 '20 at 07:11

3 Answers3

1

Because you call your method in an anonymous class (your ClickListener) the this pointer refers to the ClickListener not the Activity.

See here for an explanation: Getting hold of the outer class object from the inner class object

Change the method call to this:

Intent launchIntent = BarcodeReaderActivity.getLaunchIntent(test.this, true, false);
startActivityForResult(launchIntent, BARCODE_READER_ACTIVITY_REQUEST);

The getLaunchIntent(...) method takes an OnClickListener as first parameter. Change it to Context like this:

 public static Intent getLaunchIntent(
        Context context, boolean autoFocus, boolean useFlash) {
    Intent intent = new Intent(context, BarcodeReaderActivity.class);
    intent.putExtra(KEY_AUTO_FOCUS, autoFocus);
    intent.putExtra(KEY_USE_FLASH, useFlash);
    return intent;
}
Steve Benett
  • 12,843
  • 7
  • 59
  • 79
0

The problem is so simple, you just try to cast onClickListener to context but it is not possible. You have to pass activity reference to this method rather than onclicklistener.

M.ekici
  • 765
  • 4
  • 10
0

Try change:

Intent launchIntent = BarcodeReaderActivity.getLaunchIntent(this, true, false);

to this:

Intent launchIntent = BarcodeReaderActivity.getLaunchIntent(test.this, true, false);

besides that, please consider using correct naming conventions of your Activity class name: https://www.javatpoint.com/java-naming-conventions

etomun
  • 134
  • 6