I am just starting out with the Android SDK. I wanted to make a simple app with WebView that displays local web-pages only. I have used examples I have found on the net to make it.
The app works just great, until I want to add a back-button. When the phones back-button is pressed the app crashes.
Here is my MainActivity.java
package com.hspd.avhor;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView)findViewById(R.id.hspd_webview);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
wv.loadUrl("file:///android_asset/one.htm");
}
@Override
public void onBackPressed()
{
if(wv.canGoBack())
wv.goBack();
else
super.onBackPressed();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And here is my activity_main.xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<WebView
android:id="@+id/hspd_webview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
Here are my errors in logcat:
04-22 17:48:06.953: W/dalvikvm(799): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-22 17:48:06.972: E/AndroidRuntime(799): FATAL EXCEPTION: main
04-22 17:48:06.972: E/AndroidRuntime(799): java.lang.NullPointerException
04-22 17:48:06.972: E/AndroidRuntime(799): at com.hspd.avhor.MainActivity.onKeyDown(MainActivity.java:41)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.app.Activity.dispatchKeyEvent(Activity.java:2375)
04-22 17:48:06.972: E/AndroidRuntime(799): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.os.Looper.loop(Looper.java:137)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-22 17:48:06.972: E/AndroidRuntime(799): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 17:48:06.972: E/AndroidRuntime(799): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 17:48:06.972: E/AndroidRuntime(799): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-22 17:48:06.972: E/AndroidRuntime(799): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-22 17:48:06.972: E/AndroidRuntime(799): at dalvik.system.NativeStart.main(Native Method)
Thanks in advance :)