【问题标题】:Object reference not set to an instance of an object while initializing WebView Xamarin Android初始化 WebView Xamarin Android 时未将对象引用设置为对象的实例
【发布时间】:2015-04-23 05:15:41
【问题描述】:

我有以下代码:-

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.layoutInformation);

    WebView Body = FindViewById<WebView>(Resource.Id.BodyContentWV);-- Error here

    // Create your application here
}

layoutInformation.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">

    <android.webkit.WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/BodyContentWV" />

</LinearLayout>

初始化 WebView 时出现以下错误。谁能指出我做错了什么?

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

【问题讨论】:

  • 有 WebView 的 Layout 的名称是什么?
  • @ρяσѕρєяK 它的 layoutInformation.xml

标签: android webview xamarin


【解决方案1】:

好的,我将尝试在我的代码中显示这一点,分三点:

1) 我有一个名为:ExtendedWebViewClient 的类。它看起来像这样:

public class ExtendedWebViewClient : WebViewClient
{
    public override bool ShouldOverrideUrlLoading(WebView view, string url)
    {
        view.LoadUrl(url);
        return true;
    }
}

2) 我有一个名为 SearchWebActivity 的活动。它有资源 xml 文件和 .cs 文件。

a) 资源 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/SearchWeb_WebView" />

b) SearchWebActivity 的类 .cs 文件:

public class SearchWebActivity: Activity
{
    WebView _searchWeb_WebView;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.SearchWebActivity);
        _searchWeb_WebView= FindViewById<WebView>(Resource.Id.SearchWeb_WebView);

        setWebView();
    }

    private void setWebView()
    {
        _searchWeb_WebView.Settings.LoadWithOverviewMode = true;
        _searchWeb_WebView.Settings.UseWideViewPort = true;
        _searchWeb_WebView.Settings.BuiltInZoomControls = true;
        _searchWeb_WebView.Settings.JavaScriptEnabled = true;
        _searchWeb_WebView.ScrollbarFadingEnabled = false;
        _searchWeb_WebView.SetInitialScale(1);
        _searchWeb_WebView.SetWebViewClient(new ExtendedWebViewClient());

        _searchWeb_WebView.LoadUrl("www.google.com");
    }
}

希望它会有所帮助。

【讨论】:

    【解决方案2】:

    这里:

     <android.webkit.WebView <<<< line
      ...
     />
    

    android.webkit 导致问题,因为包是 Android.Webkit 而不是 android.webkit

    只需使用 WebView 在 xml 中添加 webview:

     <WebView <<<< line
      ...
     />
    

    【讨论】:

    • 我最初只使用 WebView 本身进行了尝试。它仍然会导致同样的问题
    • @user3034944:更改后尝试清理您的项目以重新生成 R 文件,您也可以尝试使用 Android.Webkit.WebView
    • 我确实尝试了你提到的一切
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多