Xamarin表单MainActivity OnCreate LoadApplication System.NullReferenceException:对象引用未设置为对象的实例 [英] Xamarin Forms MainActivity OnCreate LoadApplication System.NullReferenceException: Object reference not set to an instance of an object

查看:295
本文介绍了Xamarin表单MainActivity OnCreate LoadApplication System.NullReferenceException:对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将VS2015与Xamarin一起使用来创建一个多平台项目,该项目可以显示启动画面,然后在Webview中加载网页.这是我的项目结构.我正在使用如下PCL项目类型:

I am using VS2015 with Xamarin to create a multi-platform project that can show a splashscreen and then load a webpage in a webview. Here is my project structure. I am using a PCL project type as below:

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类的应用程序构造函数,该类将运行以下代码

The LoadApplication method call in the OnCreate method above loads the app constructor of the App.Xaml.cs class which runs the following code

公共应用程序() { InitializeComponent(); MainPage = new NavigationPage(new WebPageHoster(){标题=加载网页"}); }

public App() { InitializeComponent(); MainPage = new NavigationPage(new WebPageHoster() { Title = "Load Web-Page" }); }

这将显示初始屏幕,并在设置WebView的URL后返回到OnCreate方法并给出此错误

This shows the splash screen and after setting the WebView's url reaches back to the OnCreate method and gives this error

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

我找不到导致此错误的原因.这是Android Manifest文件.

I am unable to find what is causing this error. Here is the Android Manifest file.

<?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的主题.

I tested your source code and your MainActivity derives from FormsAppCompatActivity. In that case you must provide a theme that uses AppCompat.

为解决这个问题,我在您的样式中添加了AppCompat主题:

To solve that, I added an AppCompat Theme to your styles:

<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

这篇关于Xamarin表单MainActivity OnCreate LoadApplication System.NullReferenceException:对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆