I have a web app I’m making to run off a CMS that is using jQuery Mobile to make mobile compatible page versions. The base log in page is stored locally on the phone.
public class WebAppActivity extends Activity {
WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Point to the content view defined in XML
setContentView(R.layout.main);
// Configure the webview setup in the xml layout
WebView webView = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = webView.getSettings();
// Yes, we want javascript, pls.
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/www/webapp.html");
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
}
@Override
public void onBackPressed()
{
if(webView.canGoBack())
{
webView.goBack();
}else{
super.onBackPressed();
}
}
}
and the the Logcat says :
03-29 15:31:47.354: E/AndroidRuntime(430): FATAL EXCEPTION: main
03-29 15:31:47.354: E/AndroidRuntime(430): java.lang.NullPointerException
03-29 15:31:47.354: E/AndroidRuntime(430): at com.package.mobile.android.webapp.onBackPressed(WebApp.java:223)
03-29 15:31:47.354: E/AndroidRuntime(430): at android.app.Activity.onKeyUp(Activity.java:1898)
03-29 15:31:47.354: E/AndroidRuntime(430): at android.view.KeyEvent.dispatch(KeyEvent.java:1280)
03-29 15:31:47.354: E/AndroidRuntime(430): at android.app.Activity.dispatchKeyEvent(Activity.java:2078)
03-29 15:31:47.354: E/AndroidRuntime(430): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
03-29 15:31:47.354: E/AndroidRuntime(430): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)
03-29 15:31:47.354: E/AndroidRuntime(430): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)
03-29 15:31:47.354: E/AndroidRuntime(430): at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
03-29 15:31:47.354: E/AndroidRuntime(430): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 15:31:47.354: E/AndroidRuntime(430): at android.os.Looper.loop(Looper.java:123)
03-29 15:31:47.354: E/AndroidRuntime(430): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-29 15:31:47.354: E/AndroidRuntime(430): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 15:31:47.354: E/AndroidRuntime(430): at java.lang.reflect.Method.invoke(Method.java:507)
03-29 15:31:47.354: E/AndroidRuntime(430): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-29 15:31:47.354: E/AndroidRuntime(430): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-29 15:31:47.354: E/AndroidRuntime(430): at dalvik.system.NativeStart.main(Native Method)
I’ve been trying to get around this for the past week or so, it seems as if the webview isn’t building a history to go back through.
The
NullPointerExceptionis because yourwebViewis a local variable inonCreate, and thewebViewclass member variable is left unassigned.Off-topic: I won’t go into a long tirade about purely WebView-based apps, but let’s just say you shouldn’t do it. And if you absolutely have to for some reason or another, then you should make sure to handle orientation changes.