简体   繁体   中英

Error receiving broadcast Intent caused by java.lang.NullPointerException

I have this code for a Cordova plugin that tries to scan a barcode and send back the result:

public class Scan extends CordovaPlugin {

    private CallbackContext callbackContext;
    private Intent intentService = new Intent("com.hyipc.core.service.barcode.BarcodeService2D");
    private String strBarcode = "";
    BroadcastReceiver receiver;


    @Override
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
        if (action.equals("scan")) {
            scan();
            return true;
        } else {
            return false;
        }
    }

    public void scan() {

        IntentFilter filter = new IntentFilter();
        filter.addAction("action_barcode_broadcast");

        if (this.receiver == null) {
            this.receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                     if(intent.getAction().equals("action_barcode_broadcast")) {
                         strBarcode = intent.getExtras().getString("key_barcode_string");
                         callbackContext.success(strBarcode);
                     }
                 }

             };
             cordova.getActivity().startService(intentService);
             cordova.getActivity().registerReceiver(this.receiver, filter);
         }
     }
 }

When I run it, my app crashes and I get this in the logs:

07-18 08:53:28.583: E/AndroidRuntime(2345): FATAL EXCEPTION: main
07-18 08:53:28.583: E/AndroidRuntime(2345): java.lang.RuntimeException: Error receiving broadcast Intent { act=action_barcode_broadcast flg=0x10 (has extras) } in com.example.plugin.Scan$1@414f29f0
07-18 08:53:28.583: E/AndroidRuntime(2345):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:798)
07-18 08:53:28.583: E/AndroidRuntime(2345):     at android.os.Handler.handleCallback(Handler.java:800)
07-18 08:53:28.583: E/AndroidRuntime(2345):     at android.os.Handler.dispatchMessage(Handler.java:100)
07-18 08:53:28.583: E/AndroidRuntime(2345):     at android.os.Looper.loop(Looper.java:194)
07-18 08:53:28.583: E/AndroidRuntime(2345):     at android.app.ActivityThread.main(ActivityThread.java:5392)
07-18 08:53:28.583: E/AndroidRuntime(2345):     at java.lang.reflect.Method.invokeNative(Native Method)
07-18 08:53:28.583: E/AndroidRuntime(2345):     at java.lang.reflect.Method.invoke(Method.java:525)
07-18 08:53:28.583: E/AndroidRuntime(2345):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-18 08:53:28.583: E/AndroidRuntime(2345):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-18 08:53:28.583: E/AndroidRuntime(2345):     at dalvik.system.NativeStart.main(Native Method)
07-18 08:53:28.583: E/AndroidRuntime(2345): Caused by: java.lang.NullPointerException
07-18 08:53:28.583: E/AndroidRuntime(2345):     at com.example.plugin.Scan$1.onReceive(Scan.java:74)
07-18 08:53:28.583: E/AndroidRuntime(2345):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:788)
07-18 08:53:28.583: E/AndroidRuntime(2345):     ... 9 more
07-18 08:53:28.608: E/AppErrorDialog(586): Failed to get ILowStorageHandle instance
07-18 08:53:28.640: E/jdwp(2401): Failed sending reply to debugger: Success
07-18 08:53:29.221: E/AEE/DUMPSTATE(2424): copy_file: Copy /data/anr/2345.hprof to PROCESS_OOME_HPROF failed(2), No such file or directory
07-18 08:53:30.547: E/AEE/DUMPSTATE(2424): copy_file: Copy /proc/gpulog to SYS_GPU_INFO failed(2), No such file or directory
07-18 08:53:33.084: E/AEE/DUMPSTATE(2446): copy_process: execv /system/xbin/proc_mem failed(2), No such file or directory
07-18 08:53:33.097: E/AEE/DUMPSTATE(2447): copy_process: execv /system/bin/dmlog failed(2), No such file or directory
07-18 08:53:34.130: E/jdwp(2401): Failed sending reply to debugger: Success

Line 74, which the log indicates that it's causing the error, is the one in which I'm calling callbackContext.success method. I'm sure that strBarcode exists, because I can log it in the onReceive function.

You are just creating an object for CallbackContext But not initializing it by calling the default constructor for the class.. Thats y the null pointer exception is occurring

So try this way

public void scan(CallbackContext context) {
    this.callbackContext = context;
    IntentFilter filter = new IntentFilter();
    filter.addAction("action_barcode_broadcast");

    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                 if(intent.getAction().equals("action_barcode_broadcast")) {
                     strBarcode = intent.getExtras().getString("key_barcode_string");
                     callbackContext.success(strBarcode);
                 }
             }

         };
         cordova.getActivity().startService(intentService);
         cordova.getActivity().registerReceiver(this.receiver, filter);
     }
 }

This will fix the Null pointer Exception

EDIT

the issue was that the returned callbackContext and your created callbackContext are of same name.. So modify the scan()

So that it will look some thing like this

 scan(callbackContext);

and inside the function

public void scan(CallbackContext clBackCtxt) {
  this.callbackContext = clBackCtxt;

   // your rest code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM