Question

I am planing to use ZXING library to provide QR code functionality in my Android app. I am able to get it to work, but it throws an exception. I am using the sample code provided by the library github link. Can anyone let me know what is causing the exception?

Links to IntentIntegrator and IntentResult classes.

Sample code for MainActiivity (used eclipse to generate initial code):

package com.example.testapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.zxing.integration.android.IntentIntegrator;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b = (Button) findViewById(R.id.button1);

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    IntentIntegrator integrator = new IntentIntegrator(
                            MainActivity.this);
                    AlertDialog d = integrator.shareText("sample text here");
                    d.show();
                } catch (Exception e) {
                    Log.e(TAG, Log.getStackTraceString(e));
                }

            }
        });

    }

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

Exception from LogCat:

04-09 13:08:13.923: E/MainActivity(27574): java.lang.NullPointerException
04-09 13:08:13.923: E/MainActivity(27574):  at com.example.testapp.MainActivity$1.onClick(MainActivity.java:38)
04-09 13:08:13.923: E/MainActivity(27574):  at android.view.View.performClick(View.java:4633)
04-09 13:08:13.923: E/MainActivity(27574):  at android.view.View$PerformClick.run(View.java:19330)
04-09 13:08:13.923: E/MainActivity(27574):  at android.os.Handler.handleCallback(Handler.java:733)
04-09 13:08:13.923: E/MainActivity(27574):  at android.os.Handler.dispatchMessage(Handler.java:95)
04-09 13:08:13.923: E/MainActivity(27574):  at android.os.Looper.loop(Looper.java:157)
04-09 13:08:13.923: E/MainActivity(27574):  at android.app.ActivityThread.main(ActivityThread.java:5356)
04-09 13:08:13.923: E/MainActivity(27574):  at java.lang.reflect.Method.invokeNative(Native Method)
04-09 13:08:13.923: E/MainActivity(27574):  at java.lang.reflect.Method.invoke(Method.java:515)
04-09 13:08:13.923: E/MainActivity(27574):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
04-09 13:08:13.923: E/MainActivity(27574):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
04-09 13:08:13.923: E/MainActivity(27574):  at dalvik.system.NativeStart.main(Native Method)
04-09 13:08:13.933: D/AbsListView(27574): unregisterIRListener() is called 
04-09 13:08:14.518: D/AbsListView(27574): onVisibilityChanged() is called, visibility : 4
04-09 13:08:14.518: D/AbsListView(27574): unregisterIRListener() is called 

Thanks in advance.

Solution:

Thank you all for the help.

As Tanis.7x pointed out below, my error was with the line d.show().

I am trying to write the root of the issue as I understand it, correct me if am wrong.

The method shareText() returns a dialogbox if and only if it cannot find the package "com.google.zxing.client.android".

The dialog should be used to prompt the user to install the application "Barcode Reader" from playstore.

Since I already had the package (by including the jar files in my project), the method returned null and I made the (stupid) error of calling a method on that.

Was it helpful?

Solution

The shareText() method is not guaranteed to return a dialog.

As per the documentation, shareText() will return:

the AlertDialog that was shown to the user prompting them to download the app if a prompt was needed, or null otherwise

If the user already has the app, there is no need to show a dialog, and this method will return null.

Also note that the dialog will have already been shown by the time this method returns, so you do not need to show it yourself. I recommend looking through the IntentIntegrator source for more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top