Search code examples
androidwebviewcrashprivateback-button

Up Button Webview Crashes


I am trying to create a back button in my webview app. It is supposed to navigate webhistory. However, the app keeps crashing when I use the back button.

public class MainActivity extends Activity {
private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(amapps.com.uhss.R.layout.activity_main);

    mWebView.setWebViewClient(new WvClient());
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.loadUrl("http://uhsswordandshield.com/");
    mWebView.getSettings().setSupportMultipleWindows(true);


}


@Override
public void onBackPressed() {
    if (mWebView.canGoBack()) {
        mWebView.goBack();

    } else {

        // Otherwise defer to system default behavior.
        super.onBackPressed();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(amapps.com.uhss.R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();


    //noinspection SimplifiableIfStatement
    if (id == amapps.com.uhss.R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
private class WvClient extends WebViewClient{

}
}

This is my Xml File

<RelativeLayout 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:paddingTop="16dp"
android:paddingBottom="16dp"



tools:context=".MainActivity"
>
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
    />
    </RelativeLayout>

This is the error I am getting when I launch the app

EXCEPTION: main
Process: amapps.com.uhss, PID: 20498
java.lang.RuntimeException: Unable to start activity ComponentInfo{amapps.com.uhss/amapps.com.uhss.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
        at android.app.ActivityThread.access$900(ActivityThread.java:170)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5635)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at amapps.com.uhss.MainActivity.onCreate(MainActivity.java:23)
        at andr

If someone could please post a workaround this that would be great. My app work perfectly without the

private WebView mWebview

but I can't use the

onBackPressed()

method without it. I have been using the code from Android Studio's website

 public class MainActivity extends Activity {

 private WebView mWebView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 ...
 }

@Override
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   ...
}

}

Solution

  • You need to assign mWebView before you start calling methods on it:

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.setWebViewClient(new WvClient());
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("http://uhsswordandshield.com/");
    mWebView.getSettings().setSupportMultipleWindows(true);
    

    Currently you are calling a method on it before you assign it, which means it will be null at the time.