I need to load an external URL into CordovaWebview. I have the following code.
public class ActionBar extends CordovaActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); CordovaWebView webview = new CordovaWebView(this); Config.init(this); webview.loadUrl("http://www.google.com"); } }
The application crashes after a while.
Magazine Details:
08-08 12:42:44.490: E/AndroidRuntime(32712): FATAL EXCEPTION: main 08-08 12:42:44.490: E/AndroidRuntime(32712): Process: com.example.actionbar, PID: 32712 08-08 12:42:44.490: E/AndroidRuntime(32712): java.lang.NullPointerException 08-08 12:42:44.490: E/AndroidRuntime(32712): at org.apache.cordova.CordovaWebView.stopLoading(CordovaWebView.java:546) 08-08 12:42:44.490: E/AndroidRuntime(32712): at org.apache.cordova.CordovaWebView$2.run(CordovaWebView.java:468) 08-08 12:42:44.490: E/AndroidRuntime(32712): at android.os.Handler.handleCallback(Handler.java:733) 08-08 12:42:44.490: E/AndroidRuntime(32712): at android.os.Handler.dispatchMessage(Handler.java:95) 08-08 12:42:44.490: E/AndroidRuntime(32712): at android.os.Looper.loop(Looper.java:212) 08-08 12:42:44.490: E/AndroidRuntime(32712): at android.app.ActivityThread.main(ActivityThread.java:5151) 08-08 12:42:44.490: E/AndroidRuntime(32712): at java.lang.reflect.Method.invokeNative(Native Method) 08-08 12:42:44.490: E/AndroidRuntime(32712): at java.lang.reflect.Method.invoke(Method.java:515) 08-08 12:42:44.490: E/AndroidRuntime(32712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878) 08-08 12:42:44.490: E/AndroidRuntime(32712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 08-08 12:42:44.490: E/AndroidRuntime(32712): at dalvik.system.NativeStart.main(Native Method)
EDIT 1
public class GetCordovaWebview extends Activity implements CordovaInterface { protected static Activity currentActivity; private CordovaWebView cordova_webview; private String TAG = "CORDOVA_ACTIVITY"; private final ExecutorService threadPool = Executors.newCachedThreadPool(); // Android Activity Life-cycle events @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FrameLayout layout = new FrameLayout(this); CordovaWebView webview = new CordovaWebView(this); Config.init(this); layout.addView(webview); setContentView(layout); webview.loadurl("file:///android_asset/www/index.html"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume"); } @Override protected void onDestroy() { super.onDestroy(); if (this.cordova_webview != null) { this.cordova_webview .loadUrl("javascript:try{cordova.require('cordova/channel').onDestroy.fire();}catch(e){console.log('exception firing destroy event from native');};"); this.cordova_webview.loadUrl("about:blank"); this.cordova_webview.removeAllViews(); cordova_webview.handleDestroy(); } } @Override public Activity getActivity() { return this; } @Override public ExecutorService getThreadPool() { return threadPool; } @Override public Object onMessage(String message, Object obj) { Log.d(TAG, message); if (message.equalsIgnoreCase("exit")) { super.finish(); } return null; } @Override public void setActivityResultCallback(CordovaPlugin cordovaPlugin) { Log.d(TAG, "setActivityResultCallback is unimplemented"); } @Override public void startActivityForResult(CordovaPlugin cordovaPlugin, Intent intent, int resultCode) { Log.d(TAG, "startActivityForResult is unimplemented"); }
}
Tried the above lines of code. But the application crashes. When debugging a Cordova 3.5 source, the stopLoading () webview method is called, the variable WebviewClient (viewClient.isCurrentlyLoading) (viewClient) is null . Is there anything that needs to be changed to get Cordoba.
source share