扫描qr代码,传递qr内容进行查询,并在android屏幕上显示结果

时间:2013-02-19 06:24:34

标签: android qr-code zxing barcode-scanner

我正在使用zxing来扫描android中的qr代码并获得结果。现在我想将qr代码内容(总是一个数字)传递给sqlite select查询并将结果显示在屏幕上。但是当我将内容传递给查询时,我得到Null指针异常。 代码是

  Intent intent = new Intent("com.google.zxing.client.android.SCAN");
  intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
  startActivityForResult(intent, 0);

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contentId = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");                

            Cursor cursorDetails = db.query("cd_details", new String[] {"location","latitude","longitude","photo"}, "id" + " = " + "'" + contentId + "'", null, null, null, null);
            cursorDetails.moveToFirst();


        } else if (resultCode == RESULT_CANCELED) {                
            Toast toast = Toast.makeText(this, "Scan was Cancelled!", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP, 25, 400);
            toast.show();                
        }
    }
}

我在查询行中收到错误。错误读起来像这样

 java.lang.RuntimeException: Unable to resume activity {com.pixel.scanner/com.pixel.scanner.ScannerActivity}: 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 {com.pixel.scanner/com.pixel.scanner.ScannerActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2836)
at android.app.ActivityThread.access$1600(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
 Caused by: 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 {com.pixel.scanner/com.pixel.scanner.ScannerActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2111)
... 13 more
 Caused by: java.lang.NullPointerException
at com.pixel.scanner.ScannerActivity.onActivityResult(ScannerActivity.java:70)
at android.app.Activity.dispatchActivityResult(Activity.java:3908)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
... 14 more

上面代码中的第70行是

Cursor cursorDetails = db.query("cd_details", new String[] {"location","latitude","longitude","photo"}, "id" + " = " + "'" + contentId + "'", null, null, null, null);

对此有任何建议都会有很大的帮助。

2 个答案:

答案 0 :(得分:1)

堆栈跟踪已经告诉您答案。您的应用在第70行遇到问题。db为空。

答案 1 :(得分:1)

问题是我打开数据库进行READ / WRITE。因此db变为null。我添加了以下行,现在正在工作..

  db = SQLiteDatabase.openDatabase(fileIsExists.toString(), null, SQLiteDatabase.OPEN_READWRITE);