I have implemented a barcode scanner in my Android Application. I want to set the barcode image (bitmap) generated by my scanner to an ImageView in an XML file.
However, this XML file will not be initialized before I start the MatasActivity (where the Image/ImageView will be showed). This gives me a nullpointerexception, since the "imageCode" variable cannot find "R.id.imageCode" from the XML file.
How can I set the ImageView from an XML file in a new activity -- I can't do it after startActivity (since the rest of the code will not run but rather the onCreate in the new Activity)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, MatasActivity.class);
imageCode = (ImageView) findViewById(R.id.imageCode);
Bitmap myBitmap = BitmapFactory.decodeFile(result.getBarcodeImagePath());
imageCode.setImageBitmap(myBitmap);
startActivity(intent);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
The MatasActivity just have the purpose of running the XML file (to show the barcode) AKA the onCreate in MatasActivity method only performs:
setContentView(R.layout.picked_card);
Which is the XML file where imageCode is kept.