From 5289d569b06437b5b6fdd536b693f1a2d04ef224 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Tue, 2 Oct 2012 09:49:53 -0400 Subject: [PATCH] Fix NPE caused by NetworkManager sending update before JS is ready. This was happening for me when the device has been sleeping long enough to turn its networking off, and I start an app via adb. --- .../src/org/apache/cordova/NativeToJsMessageQueue.java | 4 ++++ framework/src/org/apache/cordova/NetworkManager.java | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/framework/src/org/apache/cordova/NativeToJsMessageQueue.java b/framework/src/org/apache/cordova/NativeToJsMessageQueue.java index 934088a7..54c6eed3 100755 --- a/framework/src/org/apache/cordova/NativeToJsMessageQueue.java +++ b/framework/src/org/apache/cordova/NativeToJsMessageQueue.java @@ -219,6 +219,10 @@ public class NativeToJsMessageQueue { * Add a JavaScript statement to the list. */ public void addPluginResult(PluginResult result, String callbackId) { + if (callbackId == null) { + Log.e(LOG_TAG, "Got plugin result with no callbackId", new Throwable()); + return; + } // Don't send anything if there is no result and there is no need to // clear the callbacks. boolean noResult = result.getStatus() == PluginResult.Status.NO_RESULT.ordinal(); diff --git a/framework/src/org/apache/cordova/NetworkManager.java b/framework/src/org/apache/cordova/NetworkManager.java index 8c8c4002..f473abec 100755 --- a/framework/src/org/apache/cordova/NetworkManager.java +++ b/framework/src/org/apache/cordova/NetworkManager.java @@ -206,9 +206,11 @@ public class NetworkManager extends Plugin { * @param connection the network info to set as navigator.connection */ private void sendUpdate(String type) { - PluginResult result = new PluginResult(PluginResult.Status.OK, type); - result.setKeepCallback(true); - this.success(result, this.connectionCallbackId); + if (connectionCallbackId != null) { + PluginResult result = new PluginResult(PluginResult.Status.OK, type); + result.setKeepCallback(true); + this.success(result, this.connectionCallbackId); + } webView.postMessage("networkconnection", type); }