-1

So I'm trying to build a scanner and generator for QR Code.

Here's my code for the scanner:

 private Button scan_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reader);
    scan_btn = (Button) findViewById(R.id.scan_btn);
    final Activity activity = this;
    scan_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            IntentIntegrator integrator = new IntentIntegrator(activity);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
            integrator.setPrompt("Scan");
            integrator.setCameraId(0);
            integrator.setBeepEnabled(false);
            integrator.setBarcodeImageEnabled(false);
            integrator.initiateScan();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null){
        if(result.getContents()==null){
            Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(this, result.getContents(),Toast.LENGTH_LONG).show();
        }
    }
    else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}}

I got a warning like this: Casting 'findViewById(R.id.scan_btn)' to 'Button' is redundant

Is gonna affect my app? cause when I tried to open my app, it suddenly crash.

UPDATE:

Here's my Generator code:

public class GeneratorActivity extends AppCompatActivity {

EditText text;
Button gen_btn;
ImageView image;
String text2Qr;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_generator);
    text = findViewById(R.id.text);
    gen_btn = findViewById(R.id.gen_btn);
    image = findViewById(R.id.image);
    gen_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            text2Qr = text.getText().toString().trim();
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            try{
                BitMatrix bitMatrix = multiFormatWriter.encode(text2Qr, BarcodeFormat.QR_CODE,200,200);
                BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                image.setImageBitmap(bitmap);
            }
            catch (WriterException e){
                e.printStackTrace();
            }
        }
    });
}}

My MainActivity for the 'buttons' code:

public class MainActivity extends AppCompatActivity {

Button gen, scan;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    gen = findViewById(R.id.gen);
    scan = findViewById(R.id.scan);
    gen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent gIntent = new Intent(MainActivity.this, GeneratorActivity.class);
            startActivity(gIntent);
        }
    });
    scan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent rIntent = new Intent(MainActivity.this, ReaderActivity.class);
            startActivity(rIntent);
        }
    });
}}

UPDATE: There's nothing wrong with the redundant problem. This case is solved on xml code typo mistake. So its already answered on one of my questions. Thanks. Please looking forward on my others questions. I really need it to get done for my project soon.

  • The redundant cast warning is just a warning and does not have anything to deal with crashes. To start debugging a crash, have a look at stacktrace in logcat. https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – laalto Feb 02 '18 at 18:22

3 Answers3

0

Try out this, Change your code as below :

scan_btn = findViewById(R.id.scan_btn);
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
0

It does not affect your code. This is a new feature of Android studio that directly maps the widget from your XML to Java code. Your app may crash for any other reason

0

Starting with API 26, findViewById uses inference for its return type, so you no longer have to cast.

Old definition:

Button btn = (Button) findViewById(R.id.btn)

New definition:

<T extends View> T findViewById(R.id.value)

So if your compile Sdk is at least 26, it means that you can make use of this. No need casting.

manitaz
  • 1,181
  • 2
  • 9
  • 26