Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms MainActivity OnCreate LoadApplication System.NullReferenceException: Object reference not set to an instance of an object

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:

enter image description here

TheApp (Portable)

-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)

This is the code in the 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
    }

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

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

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

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>
like image 311
user20358 Avatar asked Jun 25 '16 05:06

user20358


1 Answers

I tested your source code and your MainActivity derives from FormsAppCompatActivity. In that case you must provide a theme that uses 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> 

And then used it on your MainActivity:

[Activity(Label = "Southern Travel", Theme = "@style/AppTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
like image 186
jzeferino Avatar answered Oct 21 '22 04:10

jzeferino