user3034944
user3034944

Reputation: 1751

Object reference not set to an instance of an object while initializing WebView Xamarin Android

I have the following code:-

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>

I get the following error while initializing the WebView. Can anyone point out anything Iam doing wrong?

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

Upvotes: 0

Views: 2553

Answers (2)

Daniel Krzyczkowski
Daniel Krzyczkowski

Reputation: 3157

Ok, I will try to show this on my code, in three points:

1) I have class named: ExtendedWebViewClient. It looks like this:

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

2) I have Activity named SearchWebActivity. It has resource xml file and .cs file.

a) Resource xml file:

<?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) Class .cs file for SearchWebActivity:

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

Hope it will help.

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

Here:

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

android.webkit causing issue because package is Android.Webkit instead of android.webkit.

Simply use WebView for adding webview in xml:

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

Upvotes: 0

Related Questions