Xamarin Forms MainActivity OnCreate LoadApplication System.NullReferenceException: Object reference not set to an instance of an object
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
user20358
Updated on June 17, 2022Comments
-
user20358 almost 2 years
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 (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 theOnCreate
method above loads the app constructor of the App.Xaml.cs class which runs the following codepublic 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>
-
user20358 almost 8 yearsThank you. Thank you. Was going crazy with this. Where can I get real documentation around Xamarin Forms? It seems like wherever I look its something specific to a particular target platform. Even this official documentation[developer.xamarin.com/samples/monodroid/SplashScreen/] on the Xamarin site doesn't do this example in the PCL form:
-
Bonelol almost 8 years@user20358, setting a SpalshScreen is different between iOS and Android, this post might help for Android forums.xamarin.com/discussion/comment/91914/#Comment_91914
-
jzeferino almost 8 yearsThere is here a good tutorial on this for android and iOS: codeworks.it/blog/?p=294
-
Aadhil Ahmed about 6 yearsHi..My code is just as you have said. But still it shows null pointer exception on this line( LoadApplication(new App()); ) in mainActivity.cs.
-
jzeferino about 6 years@AadhilAhmed you should read the full stackstrace and try to understand.
-
Aadhil Ahmed about 6 yearsThe problem was with packages.config file which I found later.Thanks.