1
votes

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));
        }
    }
  1. 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?

1
First of all, calling sleep() on the main (UI) thread of an Android application will get you an ANR (Application Not Responding) crash. You absolutely positively cannot stall the main (UI) thread. - David Wasser
Second, in what method are you calling new Frame() where it crashes? Please post a stack trace. Your Context isn't properly initialized and that is what is causing the NPE. - David Wasser
Thanks for the input Dave, I have embellished my post with the info requested. - pstar
Sorry, my knowledge of Xamarin is not good. I can't help here. I've added the Xamarin tag to your question, hopefully someone who knows Xamarin will help. - David Wasser

1 Answers

1
votes

From the exception, it sounds like when new Frame() is called, the base native constructor is called with a null Context. This is probably because Uno expects ApplicationActivity to be run as MainLauncher=true. It's possible that inheriting your SplashActivity class from Uno.UI.BaseActivity could resolve the error.

A better way to show a splash screen on Android is to modify the theme rather than create a separate activity. I'll use the Uno Gallery app as an example.

  1. Create a file in the Resources/drawable folder of your Android head called, eg, splash.xml. Define your splash screen visual appearance here.

  2. Open the Resources/values/Styles.xml file. Inside the 'AppTheme' style add the following line:

    <item name="android:windowBackground">@drawable/splash</item>
    

Hope that helps. Also, please tag questions about the Uno Platform as 'uno-platform' (the 'uno' tag refers to the OpenOffice component model).