0

使用 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
4

2 回答 2

0

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

于 2013-11-07T18:39:24.493 回答
0

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

于 2013-11-07T18:42:48.717 回答