Android: ZXing Barcode Variation?

0

Good Night, I'm using the Zxing Library for a project to read barcodes and confront the bar code that is in DB within android . When it a barcode that has in DB it works but when reading another stranger it to the application and closes it. Here are the codes:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case IntentIntegrator.REQUEST_CODE:
            IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            final String result = scanResult.getContents();
            if ((scanResult.toString().contentEquals(getString(result))) && (result != null)) {
                Toast.makeText(getBaseContext(), "Código válido.", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getBaseContext(), "Código inválido ou inexistente.", Toast.LENGTH_LONG).show();
            }
            break;
        default:
    }
}
public String getString(String var) {
    String selectQuery =
            "SELECT * FROM dados WHERE codBarras ='"+var+"'";

    db = openOrCreateDatabase("infor.db3", Context.MODE_PRIVATE, null);
    cursor = db.rawQuery(selectQuery, null);

    StringBuilder sb = new StringBuilder();

    cursor.moveToFirst();

    /*********** Fazer isto por cada coluna ***************/
    String nome_da_coluna_string = cursor.getString(cursor.getColumnIndexOrThrow("codBarras"));

    sb.append(nome_da_coluna_string);
    /******************************************************/

    cursor.close();

    return sb.toString();
}

It displays the following error:

08-22 23:22:01.818  10050-10050/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: mvsistemas.com.leitor, PID: 10050
java.lang.RuntimeException: Unable to resume activity {mvsistemas.com.leitor/mvsistemas.com.leitor.ColProdActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=49374, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity {mvsistemas.com.leitor/mvsistemas.com.leitor.ColProdActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2800)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2829)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2260)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3760)
        at android.app.ActivityThread.access$900(ActivityThread.java:142)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5118)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=49374, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity {mvsistemas.com.leitor/mvsistemas.com.leitor.ColProdActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3387)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2787)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2829)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2260)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3760)
        at android.app.ActivityThread.access$900(ActivityThread.java:142)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5118)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
        at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
        at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
        at mvsistemas.com.leitor.ColProdActivity.getString(ColProdActivity.java:153)
        at mvsistemas.com.leitor.ColProdActivity.onActivityResult(ColProdActivity.java:97)
        at android.app.Activity.dispatchActivityResult(Activity.java:5467)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3383)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2787)
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2829)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2260)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3760)
        at android.app.ActivityThread.access$900(ActivityThread.java:142)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5118)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
        at dalvik.system.NativeStart.main(Native Method

Thank you for helping me.

    
asked by anonymous 23.08.2015 / 04:25

1 answer

0

Well, I do not know if it will help (I have not done it for a long time with java), but apparently the error ( android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 ) is something related to not handling when the record does not exist in BD.

Before executing an operation with the return of the query, check if it has a return:
if(cursor.getCount() >= 1){

Source: link

    
23.08.2015 / 17:21