Duggan
Duggan

Reputation: 219

Webview wont go back(Xamarin - Android C#)

Im new here just to let you all know.

I started to make a webview app for android and I wanted to allow the user to be able to go back to the previous page instead of alwasy going out of the application.

Im getting this runtime error whenever I run the app and press the back icon.

"System.NullReferenceException: Object reference not set to an instance of an object"

Its for this block of code:

 public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{ 
if (keyCode == Keycode.Back && web_view.CanGoBack()) // <-- this is the line which brings up the error
{
web_view.GoBack();

           return true;
    }

          return base.OnKeyDown(keyCode, e);
}

I suspect its something to do with the WebView.

Can anyone give me some advice on how to proceed?

Below is my source code of my little project:

using System;
using Android.App;
using Android.OS;
using Android.Webkit;
using Android.Views;

namespace MyWebApp
{
[Activity(Label = "MyWebApp", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.NoTitleBar")]
public class MainActivity : Activity
{
WebView web_view; //<-- I think this might be the issue?

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        DisplayWebpage(web_view, "http.google.com");
    }


    private WebView DisplayWebpage(WebView webview, String url)
    {
        webview = FindViewById<WebView>(Resource.Id.webView);

        webview.Settings.JavaScriptEnabled = true;
        webview.LoadUrl(url);

        webview.SetWebViewClient(new WebView_Client());

        return webview;
    }


    public class WebView_Client : WebViewClient
    {
        public override bool ShouldOverrideUrlLoading(WebView view, string url)
        {
            view.LoadUrl(url);

            return true;
        }
    }


    // BACK FUNCTIONALITY
    public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
    { 
        if (keyCode == Keycode.Back && web_view.CanGoBack())
        {
            web_view.GoBack();

            return true;
        }

        return base.OnKeyDown(keyCode, e);
    }
}
}

Upvotes: 0

Views: 1780

Answers (3)

Yksh
Yksh

Reputation: 3306

If you want to go back a page or two in a WebView, You can make use of WebView.GoBack() or WebView.GoBackOrForward(int steps) methods.

Upvotes: 0

SushiHangover
SushiHangover

Reputation: 74134

You are not assigning your WebView instance (web_view) in the return of your DisplayWebpage method:

web_view = DisplayWebpage(web_view, "http.google.com");

Then you should be able to:

public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
    if (keyCode == Keycode.Back && web_view != null) {
        try {
            if (web_view.CanGoBack()) {
                Log.Debug ("StackOverflow", "Allow browser back");
                Toast.MakeText (this, "Allow browser back", ToastLength.Short).Show();
                web_view.GoBack ();
                return true;
            }
        }
        catch (Exception ex) {
            Log.Error ("StackOverflow", ex.Message);
        }
    } else {
        Log.Error ("StackOverflow", "Null webview...");
    }
    Log.Debug ("StackOverflow", "Back button blocked");
    Toast.MakeText (this, "Back button blocked", ToastLength.Short).Show ();
    return false;
} 

Upvotes: 1

Jean Jacques1998
Jean Jacques1998

Reputation: 55

web_view.GoBack();  

I think your problem is just here, you use it before to put your value inside or before to do a new(). If you want to save your old webview in web_view . You should do something like web_view = webview somewhere before. Then do web_view.GoBack()

Upvotes: 0

Related Questions