Webview不会回头(Xamarin-Android C#)

本文关键字:Xamarin-Android 回头 Webview | 更新日期: 2023-09-27 18:26:18

我是新来的,只是想让大家知道。

我开始为android制作一个webview应用程序,我想让用户能够回到上一页,而不是总是离开应用程序。

每当我运行应用程序并按下后退图标时,我都会收到这个运行时错误。

"System.NullReferenceException:对象引用未设置为对象的实例"

它适用于这个代码块:

 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);
}

我怀疑这与WebView有关。

有人能就如何进行给我一些建议吗?

以下是我的小项目的源代码:

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);
    }
}
}

Webview不会回头(Xamarin-Android C#)

您没有在DisplayWebpage方法的返回中分配WebView实例(web_view):

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

然后你应该能够:

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;
} 
web_view.GoBack();  

我认为你的问题就在这里,你之前用它来把你的值放在里面,或者之前用它做一个new()。如果要在web_view中保存旧的web视图。你应该在之前的某个地方做一些类似web_view = webview的事情。然后执行web_view.GoBack()

如果您想在WebView中返回一两页,可以使用WebView.GoBack()WebView.GoBackOrForward(int steps)方法。