我有一个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
函数中记录它。
答案 0 :(得分:1)
您只是为CallbackContext
创建一个对象但是没有通过调用该类的默认构造函数来初始化它。
那就是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