I know some others have had this problem but I've followed the solutions and it still isn't working for me.
I've created a new application, it has 1 activity which has 1 button (scan button) and 2 textviews (which are just going to output the formatname and contents that Zxing returns at the moment).
I have followed the ScanningViaIntent tutorial but it doesn't seem to be hitting onActivityResult
Below is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final IntentIntegrator integrator = new IntentIntegrator(this);
Button btnScan = (Button) findViewById(R.id.button1);
btnScan.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
integrator.initiateScan();
}
});
}
public void OnActivityResult(int requestCode, int resultCode, Intent intent)
{
Log.i("result", "hit line");
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
if(scanResult != null)
{
System.out.println("format: " + scanResult.getFormatName());
System.out.println("contents: " + scanResult.getContents());
tv1.setText(scanResult.getFormatName());
tv2.setText(scanResult.getContents());
}
else
{
tv1.setText("ERROR");
}
}
TextView1 never says "Error" so it doesn't seem that scanResult is null and my Log.i() line is never hit so I'm thinking that onActivityResult
isn't even being hit.
Could it be to do with making IntentIntegrator final
for the OnClick() method? When I created IntentIntegrator inside OnClick(), I used getParent() to pass the Activity to the constructor but this force closed my app with a NullReferenceException inside IntentItegrator.
Am I using the library correctly?
Thanks for your time,
Poncho
You are not actually overriding the method
onActivityResult()
because you have implementedOnActivityResult()
. Your method is not being called as a result. Everything else looks about right.This is the kind of thing you catch if you use
@Override
annotations -- good habit, since it would have caught this.Where are you calling
startActivityForResult(..)
? You may want to use something like this :findTargetAppPackage :
To see a more complete example go here.
You need to get the latest classes from the repository https://github.com/zxing/zxing/tree/master/android-integration/src/main/java/com/google/zxing/integration/android
See the javadoc of the class to see how to use it. First add code to invoke the Intent:
Second, add this to your Activity to handle the result:
More info here https://github.com/zxing/zxing/wiki/Scanning-Via-Intent