I am getting a NullPointerException error when I scan a qr and a bar code using Zxing. The Zxing app works perfectly fine, but when integrated it does not. What it does is scan the image, captures it, then crashes. I think I narrowed where the null pointer is located and that is the onActivityResult() function, but I can not seem to locate where it is actually coming from. I did not alter any of the Zxing source files that are being used.
Here is my bar code code:
1 import android.app.Activity;
2 import android.content.Intent;
3 import android.net.Uri;
4 import android.net.wifi.ScanResult;
5 import android.os.Bundle;
6 import android.view.MotionEvent;
7 import android.view.View;
8 import android.view.View.OnClickListener;
9 import android.widget.TextView;
11 public class BarCode extends Activity
12 {
13 @Override
14 public void onCreate(Bundle savedInstanceState)
15 {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.barcode);
18 HandleClick hc = new HandleClick();
19 findViewById(R.id.QR).setOnClickListener(hc);
20 findViewById(R.id.Scanner).setOnClickListener(hc);
21 }
22 private class HandleClick implements OnClickListener{
23 public void onClick(View v)
24 {
25 try //to see if user does have app installed, run app
26 {
27 Intent intent = new Intent("com.google.zxing.client.android.SCAN");
28 intent.setPackage("com.google.zxing.client.android");
29 intent.putExtra("SCAN_MODE", "SCAN_MODE");
30 startActivityForResult(intent,0);
31 }
32 catch(Exception e)
33 {
34 Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android");
35 Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
36 startActivity(marketIntent);
37 }
38 }
39 }
40 public void onActivityResult(int requestCode, int resultCode, Intent intent)
41 {
42 IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
43 if (requestCode == 0)
44 {
45 TextView tvStatus = (TextView)findViewById(R.id.tvStatus);
46 TextView tvResult = (TextView)findViewById(R.id.tvResult);
47 if (resultCode == RESULT_OK)
48 {
49 String contents = intent.getStringExtra("SCAN_RESULT");
50 String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
51 String scanGoogle = "http://www.google.com/search?q=";
52 scanGoogle += scanResult.getContents();
53 Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(scanGoogle));
54 startActivity(intent1);
55 }
56 else if (resultCode == RESULT_CANCELED)
57 {
58 tvStatus.setText("Press a button to start scan");
59 tvResult.setText("Scan cancelled");
60 }
61
62 }
63 }
64 }
The error is:
11-07 12:38:07.837: E/AndroidRuntime(5749): java.lang.RuntimeException: Unable to resume activity {bookshelf.Android.Java/bookshelf.Android.Java.BarCode}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity {bookshelf.Android.Java/bookshelf.Android.Java.BarCode}: java.lang.NullPointerException
The NPE is in your handler code, in onActivityResult()
. I think you are not handling a null
Intent
or scanResult
but it's not clear as we don't have your line numbers.