繁体   English   中英

接收由java.lang.NullPointerException引起的广播Intent时出错

[英]Error receiving broadcast Intent caused by java.lang.NullPointerException

我有一个Cordova插件的代码,该插件试图扫描条形码并发送回结果:

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

当我运行它时,我的应用程序崩溃了,我在日志中得到了它:

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

第74行(该日志指示该错误导致了错误)是我在其中调用callbackContext.success方法的行。 我确定strBarcode存在,因为我可以将其记录在onReceive函数中。

您只是为CallbackContext创建一个对象,但没有通过调用该类的默认构造函数来对其进行初始化。那就是y发生了null pointer exception

所以这样子

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

这将修复Null pointer Exception

编辑

问题是返回的callbackContext和您创建的callbackContext具有相同的名称。因此,请修改scan()

这样看起来会像这样

 scan(callbackContext);

在函数内部

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

   // your rest code

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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