簡體   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