1

I use QR code scanner in Android, I want split value but i cant sent result to Main_Activity from ScanActivity,crash the program and I have Error in this below

(Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference at com.exampledemo.parsaniahardik.scanbarcodeqrdemonuts.MainActivity.onCreate(MainActivity.java:26))

Anybody can help me please?


*** MainActivity Code 
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


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

        Bundle my_split = getIntent().getExtras();
        String st = my_split.getString("tvresult1");     


        String totale = st;
        String [] parte = totale.split("-");

        part1 = parte[0];
        part2 = parte[1];
        part3 = parte[2];
        part4 = parte[3];
        part5 = parte[4];
        part6 = parte[5];

        MainActivity.Name.setText(""+part1);
        MainActivity.TNumber.setText(""+part2);
        MainActivity.date.setText(""+part3);
        MainActivity.numberOf.setText(""+part4);
        MainActivity.sum.setText(""+part5);
        MainActivity.amount.setText(""+part6);


 btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ScanActivity.class);
                startActivity(intent);
            }
        });






**ScanActivity

       package com.exampledemo.parsaniahardik.scanbarcodeqrdemonuts;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import me.dm7.barcodescanner.zbar.ZBarScannerView;

public class ScanActivity extends AppCompatActivity implements ZBarScannerView.ResultHandler {
    private ZBarScannerView mScannerView;

    String part1,part2,part3,part4,part5,part6;
    //camera permission is needed.

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        mScannerView = new ZBarScannerView(this);    // Programmatically initialize the scanner view
        setContentView(mScannerView);                // Set the scanner view as the content view
    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause
    }

    @Override
    public void handleResult(me.dm7.barcodescanner.zbar.Result result) {
        // Do something with the result here
        Log.v("kkkk", result.getContents()); // Prints scan results
        Log.v("uuuu", result.getBarcodeFormat().getName()); // Prints the scan format (qrcode, pdf417 etc.)



      // MainActivity.tvresult.setText(result.getContents());
            // onBackPressed();

        Intent mainActivity = new Intent(this,MainActivity.class);
        mainActivity.putExtra("tvresult1",result.getContents().toString());
        startActivity(mainActivity);

    }




}

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
mohammad1408
  • 43
  • 2
  • 8

2 Answers2

0

You can get result from scan activity to your main activity via onActivityResult(), you can implement it like this:

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, ScanActivity.class);
            startActivityForResult(intent, 500) //500 is our request code, we will use this to check if incoming data is what we want. You can put any integer value in it
        }
    });

ScanActivity

    @Override
public void handleResult(me.dm7.barcodescanner.zbar.Result result) {
    // Do something with the result here
    Log.v("kkkk", result.getContents()); // Prints scan results
    Log.v("uuuu", result.getBarcodeFormat().getName()); // Prints the scan format (qrcode, pdf417 etc.)

    Intent mainActivity = new Intent(this,MainActivity.class);
    mainActivity.putExtra("tvresult1",result.getContents().toString());
    setResult(Activity.RESULT_OK, mainActivity);
    finish(); //exiting your ScanActivity
}

And now, all we need to do is to override onActivityResult() in your MainActivity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

 if (requestCode == 500) { //Remember the result code we set in startActivityForResult()? This is how we identify our incoming data
     if(resultCode == Activity.RESULT_OK){  //Data is received
        String result= data.getStringExtra("tvresult1"); 
        //Your result is received
     } 
   }
}
Taseer
  • 3,432
  • 3
  • 16
  • 35
0

There are a lot of ways that you can do it.

  1. as @deepakkumer said you can use startActivityForResult();
    for more details see this link
  2. you can use interface to connect two activity.
    here is a answer that may help you
  3. static variable may help you, too.
    for that, make a class and inside the class define a static variable as below.
public class QRDB {
    private static String QRCodeData;
    public static String getData() {
        return QRCodeData;
    }
    public static void setData(String data) {
        this.QRCodeData = data;
    }
} 

then on handleResult method call setData method as below

@Override
public void handleResult(me.dm7.barcodescanner.zbar.Result result) {
    QRDB.setData(result.getContents());
    finish();
}

in onResume method of MainActivity class, you can access to QrCode data.

@Override
protected void onResume () {
    super.onResume();
    Log.v("Data ", QRDB.getData());
}

hope that it helps you.

hassan moradnezhad
  • 455
  • 2
  • 6
  • 26