My xamarins forms app is crashing immediately on startup. This only happens after the app has been sitting on for ~1 hr, closed, and then opened again. Once it crashes like this one time, the app will then crash every time I try launching it until I plug it in and debug with Visual Studio, at which point it stops crashing.
Full stack trace captured from logcat:
Time Device Name Type PID Tag Message
11-25 22:50:51.794 Google Pixel Error 21626 AndroidRuntime android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
at Xamarin.Forms.Platform.Android.AppCompat.Platform.LayoutRootPage (Xamarin.Forms.Page page, System.Int32 width, System.Int32 height) [0x00034] in <6b73296523894c3d8d57f5a8e3480a43>:0
at Xamarin.Forms.Platform.Android.AppCompat.Platform.Xamarin.Forms.Platform.Android.IPlatformLayout.OnLayout (System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x0000a] in <6b73296523894c3d8d57f5a8e3480a43>:0
at Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout (System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x00018] in <6b73296523894c3d8d57f5a8e3480a43>:0
at Android.Views.ViewGroup.n_OnLayout_ZIIII (System.IntPtr jnienv, System.IntPtr native__this, System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x00009] in <a10f61e70eeb434e952fef884856c199>:0
at (wrapper dynamic-method) System.Object.18(intptr,intptr,bool,int,int,int,int)
at md51558244f76c53b6aeda52c8a337f2c37.PlatformRenderer.n_onLayout(Native Method)
at md51558244f76c53b6aeda52c8a337f2c37.PlatformRenderer.onLayout(PlatformRenderer.java:63)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2793)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2320)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7184)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Force finishing activity com.companyname.BingoBongo/md576593553c14f400ecd2910c52fe340e6.MainActivity
Here is my App.xaml.cs (it seems to be crashing either here or before because the crashes don't show up in my AppCenter crash log):
public partial class App : Application
{
private IFirebaseAuthService _firebaseAuthService;
public App()
{
InitializeComponent();
SetUpUnity();
// no try catch around this line, but crashes were happening before it was added
_firebaseAuthService = ServiceLocator.Current.GetInstance<IFirebaseAuthService>();
}
private void StartAppStore()
{
// no try catch around this line, but crashes were happening before it was added
AppCenter.Start("android=[my-app-key];",
typeof(Analytics), typeof(Crashes));
}
private void SetUpUnity()
{
try
{
var unityContainer = new UnityContainer();
// Register Firebase Singletons
unityContainer.RegisterSingleton<IFirebaseAuthService, FirebaseAuthService>();
unityContainer.RegisterSingleton<FirebaseClient, FirebaseClientSingleton>();
// Register Services
unityContainer.RegisterType<IMessageService, FirebaseMessageService>();
unityContainer.RegisterSingleton<IAccountStoreService, AccountStoreService>();
ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(unityContainer));
} catch(Exception ex)
{
HandleStartupException(ex, "Error setting up Unity dependency injection");
}
}
private void HandleStartupException(Exception ex, string title)
{
// Added as an attempt to capture the exception, but seems to crash before reaching this method
MainPage = new NavigationPage(new ExceptionView(title, ex));
}
private async void CheckAuthAndRedirect()
{
try
{
var auth = await _firebaseAuthService.GetAuth();
if (auth == null)
{
MainPage = new NavigationPage(new LoginView());
}
else
{
MainPage = new NavigationPage(new MessageView());
}
} catch(Exception ex) {
HandleStartupException(ex, "Error refreshing authentication. Please log in again.");
}
}
protected override void OnStart()
{
StartAppStore();
CheckAuthAndRedirect();
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
My app has dependencies on firebase-database-dotnet, firebase-authentication-dotnet, Xamarin.Auth (using AccountStore to persist credentials, Microsoft.AppCenter, and Unity. I haven't made any changes in the Android specific project.
Any idea why these crashes might be happening?