0

I'm new to Android development and want to build my own QR scanning app for a marketing project that I'm working on. I started reading and trying tutorials and I found this simple explanation of BarcodeSanner implementation. I did everything as said but my app is not working. When I run it on the emulator it says that the app has crashed and it closes. Can someone help me and tell me what I've done wrong. I have used this file for IntentIntegrator from Zxing's website. Here is my MainActivity file:

    package com.example.barcodesanningapp;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends  Activity implements OnClickListener {
    private Button scanBtn;
    private TextView formatTxt, contentTxt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scanBtn = (Button)findViewById(R.id.scan_button);
        formatTxt = (TextView)findViewById(R.id.scan_format);
        contentTxt = (TextView)findViewById(R.id.scan_content);
        scanBtn.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.scan_button){
            IntentIntegrator scanIntegrator = new IntentIntegrator(this);
            scanIntegrator.initiateScan();
            }

    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanningResult != null) {
            String scanContent = scanningResult.getContents();
            String scanFormat = scanningResult.getFormatName();
            formatTxt.setText("FORMAT: " + scanFormat);
            contentTxt.setText("CONTENT: " + scanContent);
            }
        else{
            Toast toast = Toast.makeText(getApplicationContext(), 
                "No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }

    }

}

Also here is the IntentResult file:

package com.google.zxing.integration.android;

public final class IntentResult {

private final String contents;
private final String formatName;

  IntentResult(String contents, String formatName) {
  this.contents = contents;
  this.formatName = formatName;
  }

  /**
   * @return raw content of barcode
  */
   public String getContents() {
   return contents;
 }

  /**
  * @return name of format, like "QR_CODE", "UPC_A". See <code>BarcodeFormat</code> for        more format names.
   */
  public String getFormatName() {
    return formatName;
  }

  }

I had to make a small change in the IntentIntegrator file because it gave me an error in this method :

public static IntentResult parseActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE) {
  if (resultCode == Activity.RESULT_OK) {
    String contents = intent.getStringExtra("SCAN_RESULT");
    String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
    byte[] rawBytes = intent.getByteArrayExtra("SCAN_RESULT_BYTES");
    int intentOrientation = intent.getIntExtra("SCAN_RESULT_ORIENTATION", Integer.MIN_VALUE);
    Integer orientation = intentOrientation == Integer.MIN_VALUE ? null : intentOrientation;
    String errorCorrectionLevel = intent.getStringExtra("SCAN_RESULT_ERROR_CORRECTION_LEVEL");
    return new IntentResult(contents,
                            formatName,
                            rawBytes,
                            orientation,
                            errorCorrectionLevel);
  }
  return new IntentResult();
}
return null;

}

I changed it to this:

return new IntentResult(contents, formatName);
   }
   return new IntentResult(null, null);

I hope someone can explain it to me in a simple way so I can understand it and fix it. Thank you for your help.

P.S EDIT:

Here is the content of the LogCat after running the app:

06-03 14:01:19.254: D/AndroidRuntime(1809): Shutting down VM 06-03 14:01:19.254: W/dalvikvm(1809): threadid=1: thread exiting with uncaught exception (group=0xb3033180) 06-03 14:01:19.254: E/AndroidRuntime(1809): FATAL EXCEPTION: main 06-03 14:01:19.254: E/AndroidRuntime(1809): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.barcodesanningapp/com.example.barcodesanningapp.MainActivity}: java.lang.NullPointerException 06-03 14:01:19.254: E/AndroidRuntime(1809): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 06-03 14:01:19.254: E/AndroidRuntime(1809): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 06-03 14:01:19.254: E/AndroidRuntime(1809): at android.app.ActivityThread.access$600(ActivityThread.java:123) 06-03 14:01:19.254: E/AndroidRuntime(1809): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 06-03 14:01:19.254: E/AndroidRuntime(1809): at android.os.Handler.dispatchMessage(Handler.java:99) 06-03 14:01:19.254: E/AndroidRuntime(1809): at android.os.Looper.loop(Looper.java:137) 06-03 14:01:19.254: E/AndroidRuntime(1809): at android.app.ActivityThread.main(ActivityThread.java:4424) 06-03 14:01:19.254: E/AndroidRuntime(1809): at java.lang.reflect.Method.invokeNative(Native Method) 06-03 14:01:19.254: E/AndroidRuntime(1809): at java.lang.reflect.Method.invoke(Method.java:511) 06-03 14:01:19.254: E/AndroidRuntime(1809): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 06-03 14:01:19.254: E/AndroidRuntime(1809): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-03 14:01:19.254: E/AndroidRuntime(1809): at dalvik.system.NativeStart.main(Native Method) 06-03 14:01:19.254: E/AndroidRuntime(1809): Caused by: java.lang.NullPointerException 06-03 14:01:19.254: E/AndroidRuntime(1809): at com.example.barcodesanningapp.MainActivity.onCreate(MainActivity.java:34) 06-03 14:01:19.254: E/AndroidRuntime(1809): at android.app.Activity.performCreate(Activity.java:4465) 06-03 14:01:19.254: E/AndroidRuntime(1809): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 06-03 14:01:19.254: E/AndroidRuntime(1809): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 06-03 14:01:19.254: E/AndroidRuntime(1809): ... 11 more 06-03 14:01:19.693: I/dalvikvm(1809): threadid=3: reacting to signal 3 06-03 14:01:19.693: I/dalvikvm(1809): Wrote stack traces to '/data/anr/traces.txt' 06-03 14:01:19.975: I/dalvikvm(1809): threadid=3: reacting to signal 3 06-03 14:01:19.975: I/dalvikvm(1809): Wrote stack traces to '/data/anr/traces.txt'

Atanas44
  • 3
  • 3

1 Answers1

0

I have been using the same stuff and it works perfectly. Could you post the complete activity from where you start the scanIntegrator? Post the strackTrace

The emulator does not have a camera by default, maybe it is crashing because it can't find a camera.

The problem is the following: - Your Activity implements OnClickListener, I can only assume this is because you want to press a button? - also scanBtn.setOnClickListener(this); returns null; You should check that scanBtn is not null.

This is the right way to add a clicklistener to a button

scanBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            IntentIntegrator scanIntegrator = new IntentIntegrator(this);
            scanIntegrator.initiateScan();
        }
    });
JorenV
  • 352
  • 3
  • 16