Search code examples
androidandroid-intentjunction

Issue using intents on Android


I have an activity which behaves like this: A barcode scanner is called via an IntentIntegrator object, it reads an URI. Then I want to pass the yielded URI into an object called JunctionInterface via its method connect().

private URI uri=null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    IntentIntegrator integrator = new IntentIntegrator(this);
    integrator.initiateScan();
    if(uri!=null) {
        JunctionInterface.connect(JunctionInterface.JX,this, uri);
    }
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
  if (scanResult != null) {
     try {
        uri=new URI(scanResult.getContents());
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
  }
}

When I debug I can see that the uri is clearly retrieved when calling uri=new URI(scanResult.getContents()). Unfortunately, JunctionInterface.connect() is not triggered. Even worse, when I try to put something as simple as int i=0 after integrator.initiateScan() (which mainly uses activity.startActivityForResult(intentScan, REQUEST_CODE)), the application forces close.

What is the matter with this code ? Is there a way to fix it and make it behave like I want ? Thanks.


Solution

  • Try this also. I simply moved a part of your code in other place.

    private URI uri=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        IntentIntegrator integrator = new IntentIntegrator(this);
        integrator.initiateScan();
    }
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
      if (scanResult != null) {
         try {
            uri=new URI(scanResult.getContents());
            if(uri!=null) {
                JunctionInterface.connect(JunctionInterface.JX,this, uri);
            }
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
      }
    }