Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap/Android, open ChildBrowser from .shouldOverrideUrlLoading()

I'm packaging a mobile website (over the network) in PhoneGap and would like to intercept certain links and open them using the ChildBrowser plugin. Is it possible to trigger ChildBrowser from native code (I've already determined which links to intercept)

I've tried below code but it results in NULLPointerException:

ChildBrowser childBrowser = new ChildBrowser();
childBrowser.openExternal(url, true);

Exception Details:

07-29 10:49:52.632      947-947/com.abc.androidTablet E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.phonegap.plugins.childBrowser.ChildBrowser.openExternal(ChildBrowser.java:127)
at com.mygola.androidTablet.abc_android$1.shouldOverrideUrlLoading(abc_android.java:96)
at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:270)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:372)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
like image 618
wakeup Avatar asked Nov 02 '22 17:11

wakeup


2 Answers

Without your logcat it's hard to give you further advice using the plugin but if you are calling openExternal from the native layer there's no reason to use the plugin. If you take a look on the source code you'll see all it does is take your url and load it in a new DroidGap page.

If you take only the essential part it might help tracking down where the exception is happening.

function void openExternalUrl() { 

  intent = new Intent().setClass(this.ctx.getContext(), org.apache.cordova.DroidGap.class);
  intent.setData(Uri.parse(url)); // This line will be removed in future.
  intent.putExtra("url", url);
  this.ctx.startActivity(intent);

 }

EDIT

You're getting a null exception on the context variable. I don't know why it's null at the time but you can easily fix that modifying the method and passing the current context. Replace this.cordova with context and you should be fine. Call this one from your activity pasing this as the last parameter (ex: childBrowser.openExternal("http://www.google.com", true,this)).

public String openExternal(String url, boolean usePhoneGap,Context context) {
   ...
  intent = new Intent().setClass(context, org.apache.cordova.DroidGap.class);
   ...
  context.startActivity(intent);
  }

Also to prevent errors from other calls to this function add another method with the original parameters:

public String openExternal(String url, boolean usePhoneGap) {
   openExternal(url,usePhoneGap,this.cordova.getActivity()); 
}
like image 123
caiocpricci2 Avatar answered Nov 12 '22 16:11

caiocpricci2


At the end below code worked for me...

ChildBrowser childBrowser = new ChildBrowser();
childBrowser.cordova = this;
childBrowser.showWebPage(url, null);
like image 26
wakeup Avatar answered Nov 12 '22 16:11

wakeup