ZXing扫描仪搜索结果

时间:2016-02-12 11:15:39

标签: java android zxing

我在Android应用程序中遇到了ZXing条码扫描程序的一些问题。昨天我问了一个问题here,它解释了我遇到的空指针错误。问题已经解决,只是显示光标没有找回任何信息。

我的问题是我知道查询是正确的,我知道代码是正确的,因为我已经在没有扫描仪的情况下单独测试了它并且一切正常。是否有可能我在条形码扫描仪意图中对数据库进行了错误的调用,或者我是否应该以某种方式进行操作?

任何帮助或指导都会很棒!

onActivity Result

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            //get the extras that are returned from the intent
            barcode_number = intent.getStringExtra("SCAN_RESULT");
            String bNumber = barcode_number.toString();

            //Code to add scanned item to DB, needs barcode data before can be ruin
            final Cursor barInformation = adapter.barcodeInfo(barcode_number);

            barInformation.moveToFirst();
            String itemName = barInformation.getString(barInformation.getColumnIndex("barcode_item_name"));
            Toast toast = Toast.makeText(this, "Barcode Number:" + itemName ,Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}

查询

public Cursor barcodeInfo(String number){

// Safe check to make sure db and number is not null
if(db == null || number == null){

     return null;
}

return db.rawQuery("select _id, barcode_item_name, barcode_measurement, barcode_unit from barcode where barcode_number = ?", new String[]{number});
}

错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
02-12 11:15:07.544 12808-12808/com.example.rory.prototypev2 E/AndroidRuntime:     at com.example.rory.prototypev2.DBMain.barcodeInfo(DBMain.java:437)
02-12 11:15:07.544 12808-12808/com.example.rory.prototypev2 E/AndroidRuntime:     at com.example.rory.prototypev2.scanner.onActivityResult(scanner.java:77)

2 个答案:

答案 0 :(得分:0)

试试这个

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case IntentIntegrator.REQUEST_CODE: {
            if (resultCode != RESULT_CANCELED) {
                IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
                String strData = scanResult.getContents(); // use this
            } else {
                Toast.makeText(this, "Error message", Toast.LENGTH_LONG).show();
            }
            break;
        }
    }
}

答案 1 :(得分:0)

我已经解决了这个问题。我的问题是我在调用函数之前没有打开数据库,因此函数是一个空对象。正确的代码如下。

            adapter.open();  // I was missing this line
            Cursor c = adapter.barcodeInfo(bNumber);
            c.moveToPosition(-1);
            while(c.moveToNext())
            {
                name = c.getString(c.getColumnIndex("barcode_item_name"));
                measurement = c.getInt(c.getColumnIndex("barcode_measurement"));
                unit = c.getString(c.getColumnIndex("barcode_unit"));

                Toast toast = Toast.makeText(this, "Adding" + name + "to Kitchen", Toast.LENGTH_SHORT);
                toast.show();
            }