2015-04-23 46 views
0

我有下面的代碼: -對象引用不設置到對象的實例,同時初始化的WebView Xamarin的Android

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。任何人都可以指出任何Iam做錯了什麼?

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

什麼是有WebView的佈局的名稱? –

+0

@ρяσѕρєяK其layoutInformation.xml – user3034944

+0

@ρяσѕρєяK任何幫助? – user3034944

回答

0

這裏:

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

android.webkit引起問題,因爲包是Android.Webkit而不是android.webkit

只需使用的WebView在XML中添加的WebView:

<WebView <<<< line 
    ... 
/> 
+0

我最初只用WebView iteself嘗試過。它仍然會導致相同的問題 – user3034944

+0

@ user3034944:更改後嘗試清理您的項目以重新生成R文件,也可以嘗試它爲'Android.Webkit.WebView' –

+0

我嘗試了你所提到的一切 – user3034944

1

好吧,我會盡量表現出這對我的代碼,在三點:

1)我有一個名爲類:ExtendedWebViewClient。它看起來像這樣:

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

2)我有Activity名爲SearchWebActivity。它有資源xml文件和.cs文件。

一)資源的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)類的.cs文件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"); 
    } 
} 

希望這將有助於。

相關問題