空指针异常Zxing

时间:2013-11-07 18:02:52

标签: android nullpointerexception barcode zxing qr-code

当我使用Zxing扫描qr和条形码时,我收到NullPointerException错误。 Zxing应用程序工作得非常好,但是在集成时它没有。它的作用是扫描图像,捕获图像然后崩溃。我想我缩小了空指针所在的位置,这就是onActivityResult()函数,但我似乎找不到它实际来自哪里。我没有改变任何正在使用的Zxing源文件。

这是我的条形码代码:

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    }

错误是:

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

2 个答案:

答案 0 :(得分:0)

将结果保存在onActivityResult中并尝试在onResume中调用新的Intent。

答案 1 :(得分:0)

NPE位于onActivityResult()的处理程序代码中。我认为您没有处理null IntentscanResult,但由于我们没有您的行号,因此不清楚。