ZXing扫描仪搜索结果

ZXing Scanner Searching for Results

我的 android 应用程序中的 ZXing 条码扫描器出现一些问题。我昨天问了一个问题 ,它解释了我遇到的空指针错误。问题已解决,只是显示游标没有检索回任何信息。

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

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

onActivity 结果

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)

这样试试

@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;
        }
    }
}

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

            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();
            }