I’m facing a weird problem with my app:
When pushing the menu button, my app crashes. BUT! only when i launch the app in webview!
this works :
super.loadUrl("http://www.url.com");
menu loads here and all is fine, but since i can’t get “shouldOverrideUrlLoading” to work using super.loadurl, i’m trying to launch the app straight into webview.
mwebview.loadurl("http://url.com");
does work, but if i push the menu button, it will crash.
my menu is made programmatically like so :
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Menu that appears when menu button is pressed on device
Menu m_menu = menu;
m_menu.add(Menu.NONE, Menu.FIRST+6, 0, "Hjem");
m_menu.add(Menu.NONE, Menu.FIRST+7, 0, "Om");
m_menu.add(Menu.NONE, Menu.FIRST+5, 0, "Avslutt");
return super.onCreateOptionsMenu(menu);
}
Heres my activity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
super.setBooleanProperty("loadInWebView", true);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.requestFocusFromTouch();
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.setWebChromeClient(new WebChromeClient());
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
super.setIntegerProperty("splashscreen", R.drawable.splash);
super.setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.loadUrl("http://www.url.com/app/index");
//mWebView.loadurl("url");
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Heres the error in catlog :
03-03 10:00:26.893: E/AndroidRuntime(350): FATAL EXCEPTION: main
03-03 10:00:26.893: E/AndroidRuntime(350): java.lang.NullPointerException
03-03 10:00:26.893: E/AndroidRuntime(350): at com.phonegap.DroidGap.postMessage(DroidGap.java:943)
03-03 10:00:26.893: E/AndroidRuntime(350): at com.phonegap.DroidGap.onCreateOptionsMenu(DroidGap.java:1374)
03-03 10:00:26.893: E/AndroidRuntime(350): at blogreel.app.HelloAppActivity.onCreateOptionsMenu(HelloAppActivity.java:44)
03-03 10:00:26.893: E/AndroidRuntime(350): at android.app.Activity.onCreatePanelMenu(Activity.java:2148)
03-03 10:00:26.893: E/AndroidRuntime(350): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:305)
03-03 10:00:26.893: E/AndroidRuntime(350): at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:550)
03-03 10:00:26.893: E/AndroidRuntime(350): at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1192)
03-03 10:00:26.893: E/AndroidRuntime(350): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1648)
03-03 10:00:26.893: E/AndroidRuntime(350): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
03-03 10:00:26.893: E/AndroidRuntime(350): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
03-03 10:00:26.893: E/AndroidRuntime(350): at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
03-03 10:00:26.893: E/AndroidRuntime(350): at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 10:00:26.893: E/AndroidRuntime(350): at android.os.Looper.loop(Looper.java:123)
03-03 10:00:26.893: E/AndroidRuntime(350): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-03 10:00:26.893: E/AndroidRuntime(350): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 10:00:26.893: E/AndroidRuntime(350): at java.lang.reflect.Method.invoke(Method.java:521)
03-03 10:00:26.893: E/AndroidRuntime(350): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-03 10:00:26.893: E/AndroidRuntime(350): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-03 10:00:26.893: E/AndroidRuntime(350): at dalvik.system.NativeStart.main(Native Method)
So im asking :
Any way to make my menu work when loading straight into webview?
Or to make “Shouldoverrideurl” work on all urls when using “super.loadurl”
edit
Have to add that the first page super.loadurl loads does work. the specified url has links that, when clicked, redirects within the same webview. however, the second page’s links will trigger in a new browser.
Any pointers are greatly appreciated 🙂
This is how I have managed to add menu button support to PhoneGap and regular Android Webview projects