DROID: Unhandled Exception - System.InvalidOperationException: Call Forms.Init() before HideKeyboard

BoscoBosco USUniversity ✭✭

I just finished upgraded an existing XF Android/iOS app to the latest version of XF (2.3.3.175). The Android app is compiling/targeting the latest platform (7.0 - Nougat) with a minimum target of API 19 (4.4 - KitKat),

Everything works fine on iOS. However, on Android, when I launch the mobile app, the splash screen displays and I get a message box "Unfortunately, APP NAME has stopped." - when I click OK, the app continues loading, and works fine from there on.

If I try to debug the app, an Unhandled Exception is thrown with the exception message above in the title - Call Forms.Init() before HideKeyboard, and the app terminates completely. This happens BEFORE ANY OF MY CODE IS EXECUTED (if I start the app in debug mode by pressing F11, this exception is thrown before stepping in to any of my code).

This occurs on both a Samsung Galaxy S4 running Android 5.0.1, and also on an LG VS890 running Android 4.4.2 (I don't have any newer devices to test on).

The debugging log is attached... any ideas?

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @Bosco

    Let's see your droid project MainActivity.cs code.
    Based on that error you're doing something to kind the keyboard before you call Xamarin.Forms.Init()

    I'm guessing you get occasional startup instances where the keyboard is still up when it shouldn't be, or you wouldn't be trying to hide the keyboard. Instead of fixing it in one platform in a specific way you might try something more generic in your PCL, like setting the focus to the form/page itself. A page doesn't get input therefore the keyboard drops on all platforms.

  • BoscoBosco USUniversity ✭✭

    Hi @ClintStLaurent,

    I've searched my entire solution codebase - there is no reference to HideKeyboard, nor is there any code attempting to hide the keyboard.

    If I put a breakpoint on the first line of the OnCreate(Bundle bundle) method in MainActivity.cs and debug the code, the exception is thrown ever before it hits the breakpoint, so I don't think the problem is in my code... I think the problem stems from upgrading to XF 2.3.3.175.

    I've attached MainActivity.cs for your perusal - thanks for responding :+1:

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @Bosco
    I've been running 2.3.3.175 since around the 2nd day it came out.

    If you create a new "Welcome to Xamarin" solution from scratch does it still happen?

  • BoscoBosco USUniversity ✭✭

    @kartalturgut Thank you! That was the resolution I needed.

    I was using SplashActivity, but the code I had (which had worked for more than 18 months and 4 releases) was:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        StartActivity(typeof(MainActivity));
    }
    
  • madhav.shenoy83madhav.shenoy83 AUMember ✭✭

    @kartalturgut +1. Worked for me too. @Bosco you should mark @kartalturgut response as the answer.

  • Tro_SoloTro_Solo DEMember ✭✭

    I ran into the same exception today, but the solution @kartalturgut provided did not work out for me. By looking at the StackTrace of my exception I noticed that this might be Xamarin.Forms related, as the Xamarin.Forms.Platform.Android.FormsAppCompatActivity from which my SplashActivity derived, internally calls a helper method to hide the on screen keyboard of the device when switching Activities. Apparently, Xamarin.Forms isn't ready until the call to Forms.Init(), hence the exception.
    Then I realized, that the main reason for the SplashScreen was to natively display something to the user before even calling Forms.Init(), so I don't have any need for using Xamarin.Forms.Platform.Android.FormsAppCompatActivity as subclass and switched to Android.Support.V7.App.AppCompatActivity which indeed fixed everything.

    TL;DR:
    Replace public class SplashActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    with public class SplashActivity : Android.Support.V7.App.AppCompatActivity and you're good to go.

  • JohnHardmanJohnHardman GBUniversity mod

    @mshenoyOcean - regarding "@Bosco you should mark @kartalturgut response as the answer". Unfortunately, I think you can only do this if the original post was a Question rather than a Discussion. It looks like @Bosco has put a Like on the answer, which is the best that can be done :-)

  • RaymondKellyRaymondKelly USMember ✭✭✭
    edited June 2017

    @Tro_Solo @kartalturgut I am having the same issue and cannot figure this out. Its seems to have started happening after upgrading to XF pre3.

    This only appears to be happening on API level 19:

    Here is my splash screen that I have had for years:

    `[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]

    public class SplashActivity : Activity
    {
        static readonly string TAG = "X:" + typeof(SplashActivity).Name;
    
        public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
        {
            base.OnCreate(savedInstanceState, persistentState);
            Log.Debug(TAG, "SplashActivity.OnCreate");
        }
    
        protected override void OnResume()
        {
            base.OnResume();
            StartActivity(new Intent(Application.Context, typeof(MainActivity)));
        }
    }`
    

    And the crash dump:
    Caused by android.runtime.JavaProxyThrowable: System.InvalidOperationException: Call Forms.Init() before HideKeyboard at Xamarin.Forms.Platform.Android.KeyboardManager.HideKeyboard (Android.Views.View inputView, System.Boolean overrideValidation) [0x00007] in :0 at Xamarin.Forms.Platform.Android.FormsApplicationActivity.OnPause () [0x00000] in :0 at Android.App.Activity.n_OnPause (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <49ba876635b34c88a5851b953ebab347>:0 at (wrapper dynamic-method) System.Object:f0520223-7a77-4eb5-88ef-f5addd19de4c (intptr,intptr)

  • Tro_SoloTro_Solo DEMember ✭✭

    @RaymondKelly First of all, if you're targeting Android API level 19 (KitKat 4.4), then you can't use the method Activity.OnCreate(Cundle savedInstanceState, PersistableBundle persistentState) because this method has been added in API 21 (Lollipop) https://developer.android.com/reference/android/app/Activity.html#onCreate&#40;android.os.Bundle, android.os.PersistableBundle&#41; Instead, you should change the method signature by removing the second parameter PersistableBundle persistentState entirely. The corresponding method has been around since the first version of the Android SDK (API level 1), so this won't introduce any problems.

    The stack trace isn't very helpful (as usual with Xamarin apps...), but it seems that the onPause() method of the Android Activity is being called through from a FormsApplicationActivity, which might be due to an invalid type hierarchy. Can you assure that your SplashActivity is deriving from the Android.App.Activity class? Can you show us your import declarations?
    Is this error only happening on devices/emulators running API level 19 or below? If so, it might be solely due to the not available method and the error message is simply misleading.

Sign In or Register to comment.