Click here to Skip to main content
14,934,114 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello comunity! As you can see I get an Error code if I try to debug the Activity of my xamarin program. And the problem is, that the view is NULL. But i have no clue why? See my code here:

[Activity(Label = "WebViewOpen", MainLauncher = false)]
public class WebViewOpen : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        
        base.OnCreate(savedInstanceState);
        //var view = FindViewById<WebView>(Resource.Id.webView1);
        var webView = FindViewById<WebView>(Resource.Id.webView1);

        //WebView webView = FindViewById<WebView>(Resource.Id.webView1);
        
        webView.Settings.JavaScriptEnabled = true;
        webView.Settings.BuiltInZoomControls = true;
        webView.Settings.SetSupportZoom(true);
        webView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
        webView.ScrollbarFadingEnabled = false;
        webView.SetWebViewClient(new WebViewClient());

        using (var _client = new HttpClient())              //neu
        {
            //SetContentView(Resource.Layout.home);
            _client.BaseAddress = new Uri("https://a***/");

            
            
            view.SetWebViewClient(new WebViewClient());

            //String postData = "uname=101670&pwd=hallowelt2018";

            _client.BaseAddress = new Uri("https://a***/");

            
            using (var client = new HttpClient())
            {
                var content2 = new FormUrlEncodedContent
                (new[] { new KeyValuePair<string, string>("uname", "***"),
                new KeyValuePair<string, string>("pwd", "***"),
                new KeyValuePair<string, string>("action", "login"),
                new KeyValuePair<string, string>("src", "app") });
                client.BaseAddress = new Uri("https://a***/");
                var response2 = client.PostAsync("/", content2).Result;
                string result2 = response2.Content.ReadAsStringAsync().Result;

                //webView.LoadData(result2, "text/html", "UTF8");
                
            }
        }
    }
    
}


What I have tried:

var view = FindViewById<WebView>(Resource.Id.webView1);


WebView webView = FindViewById<WebView>(Resource.Id.webView1);
Posted
Updated 1-Aug-18 20:54pm
v2
Comments
F-ES Sitecore 1-Aug-18 4:38am
   
We don't even know what line the error happens on so we know less than you.
Richard MacCutchan 1-Aug-18 4:51am
   
Show your Resource XML.
Member 13917889 1-Aug-18 4:53am
   
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="10px"
android:minHeight="10px">
<ImageButton
android:src="@drawable/logtrans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/logoutbutton"
android:layout_marginLeft="1110px" />
<ImageButton
android:src="@drawable/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/settingsbutton"
android:layout_marginLeft="1050px"
android:layout_marginTop="0.0dp" />
<android.webkit.WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView1" />
Jochen Arndt 1-Aug-18 5:40am
   
I'm not an Android developer so I have not much insight.

But it might be the same problem as described at
findViewById() not working in a not MainActivity class

You might also use a different method as described at
No More findViewById

1 solution

That exception is thrown when you try to use a property or method of an object when it has not been instantiated with the NEW keyword (assuming that is C# you are using).

You have not shown all the code, so it is not clear where you have gone wrong, but this should be relatively easy to figure out if you study how to instantiate objects.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900