[英]Xamarin Android KitKat Object reference not set to an instance of an object
[英]Xamarin Forms MainActivity OnCreate LoadApplication System.NullReferenceException: Object reference not set to an instance of an object
我正在使用VS2015與Xamarin一起創建一個多平台項目,該項目可以顯示啟動畫面,然后在webview中加載網頁。 這是我的項目結構。 我正在使用PCL項目類型如下:
TheApp(便攜式)
-WebPageHoster.Xaml //Contains a WebView control
-WebPageHoster.Xaml.cs //sets the WebView controls source property to load a webpage
-App.Xaml
-App.Xaml.cs
TheApp.Droid
/Resources/drawable/splashlogo.png
/Resources/drawable/icon3.png
/Resources/values/Styles.xml
-MainActivity.cs
-SplashActivity.cs
TheApp.iOS
TheApp.WindowsPhone(Windows Phone 8.1)
這是Styles.xml
的代碼
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowBackground">@drawable/splashlogo</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
SplashActivity.cs
[Activity(MainLauncher = true, NoHistory = true, Theme = "@style/Theme.Splash", Icon = "@drawable/icon3")]
public class SplashActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var intent = new Intent(this, typeof(MainActivity));
StartActivity(intent);
Finish();
}
}
MainActivity.cs
[Activity(Label = "Splash App", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App()); // << Problem here
}
上面的OnCreate
方法中的LoadApplication
方法調用加載App.Xaml.cs類的app構造函數,該類運行以下代碼
public App(){InitializeComponent(); MainPage = new NavigationPage(new WebPageHoster(){Title =“Load Web-Page”}); }
這將顯示啟動畫面,並在設置WebView的url后返回到OnCreate方法並提供此錯誤
System.NullReferenceException: Object reference not set to an instance of an object
我無法找到導致此錯誤的原因。 這是Android Manifest文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="TheApp.TheApp" android:installLocation="auto" android:versionCode="1">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<application android:icon="@drawable/icon3" android:theme="@style/Theme.AppCompat" android:label="Splash Web Host"></application>
</manifest>
我測試了您的源代碼,您的MainActivity
派生自FormsAppCompatActivity
。 在這種情況下,您必須提供使用AppCompat
的主題。
為了解決這個問題,我在你的樣式中添加了一個AppCompat
主題:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
</style>
然后在MainActivity
上使用它:
[Activity(Label = "Southern Travel", Theme = "@style/AppTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.