com.google.zxing.client.android.result return a Null pointer exception

257 Views Asked by At

i am trying to develop a simple app that read a QR code with zxing and redirect to the corresponding link. I do the MainActivity Class, with methods on create and onActivityResult, but when i scan a QR code my app crashes....and i dunno why can anyone help me? i post my MainActivity class, if it can help...

Thanks to all

Fabrizio

-------- MainActivity------------

public class MainActivity extends Activity {

// static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
private Button button;
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {

    final Context context = this;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.go_direct);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(context, WebViewActivity.class);
            startActivity(intent);
        }

    });

}

public void scanNow(View view) {

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

}

@SuppressLint("SetJavaScriptEnabled") 
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");

            Log.i("intent", intent.toString());
            Log.i("scan result", intent.getStringExtra("SCAN_RESULT"));

            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            Log.i("xZing", "contents: " + contents + " format: " + format); // Handle
                                                                // successful
                                                                            // scan
            setContentView( R.layout.web_view);
            webView = (WebView) findViewById(R.id.webView1);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebViewClient(new MyWebViewClient());
            webView.loadUrl(contents);
        } else if (resultCode == RESULT_CANCELED) { // Handle cancel
            Log.i("xZing", "Cancelled");
        }
    }
}

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

There are 0 best solutions below