I am trying to implement an splash screen for the Android part of my UNO solution. I can get the splash screen to appear, wait a few seconds but upon navigation to the main page I get the following exception in the app.cs
protected override void OnLaunched(LaunchActivatedEventArgs e) { #if DEBUG if (System.Diagnostics.Debugger.IsAttached) { // this.DebugSettings.EnableFrameRateCounter = true; } #endif Frame rootFrame = Windows.UI.Xaml.Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); <<<<< exception
Unhandled Exception: Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference occurred
The stack trace is as simple as :
0x25 in Uno1.App.OnLaunched at C:\Users\pjsta\Documents\Visual Studio 2017\Projects\Uno1\Uno1\Uno1.Shared\App.xaml.cs:55,17 C#
The relevant part of the solution are 1. my new SplashActivity in the Android project
[Activity(Label = "SplashScreen", MainLauncher = true, Theme = "@style/Theme.SplashActivity")]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
System.Threading.Thread.Sleep(1000);
StartActivity(typeof(MainActivity));
}
}
Modification to MainActivity to not be the MainLauncher
[Activity( MainLauncher = false, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize, WindowSoftInputMode = SoftInput.AdjustPan | SoftInput.StateHidden )]
public class MainActivity : Windows.UI.Xaml.ApplicationActivity { }
The relevant style loads fo the splash screen OK. Switching the MainActivity back to MainLauncher=true works , albeit without a splash screen. I am a newbie to Xamarin and Android development, but competent in UWP. Anyone out there got any ideas?
sleep()on the main (UI) thread of anAndroidapplication will get you an ANR (Application Not Responding) crash. You absolutely positively cannot stall the main (UI) thread. - David Wassernew Frame()where it crashes? Please post a stack trace. YourContextisn't properly initialized and that is what is causing the NPE. - David Wasser