Application windows are expected to have a root view controller at the end of application launch!

PureKromePureKrome AUMember ✭✭
edited October 2015 in Xamarin.Forms

Cry.

Suddenly my app I'm working on doesn't work anymore -> I'm now getting the following crash when I start my app:

Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Application windows are expected to have a root view controller at the end of application launch

Now, I've read the Stable release notes which say this

[Upstream change] [iOS] iOS 9 has stricter requirements for setting a RootViewController. "Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Application windows are expected to have a root view controller at the end of application launch". Possible workaround: Set the Window.RootViewController property in the AppDelegate.FinishedLaunching() method. (See the troubleshooting guide for additional details.)

Ok . not good nor do I know why ...

Checking the guide for help .. it says this :

This is error is being raised because app Windows are expected to have a Root View Controller at the end of application launch and your existing app doesn't.
Setup RootViewController property of app Window in FinishedLaunching method in AppDelegate class to point to a View Controller in your app's UI.

Ok .. so I want to try this but I have no idea what View Controller should I use? In fact, I have no idea if i even have any!

I've only got a few classes which inherit from ContentPage. I've not manually made any View Controllers :(

Can anyone help, please? It's very frustrating because I cannot run the simulator anymore to test my app!

Answers

  • PaulDistonPaulDiston USUniversity ✭✭✭✭
    edited October 2015

    Hi,

    Having had to deal with the exact same problem for the last 2 hours, we believe the issue is the following lines of code in the AppDelegate:-

    _window = new UIWindow((CGRect)UIScreen.MainScreen.Bounds); _window.MakeKeyAndVisible();

    If you have these, which we did as they were part of the original AppDelegate in an earlier version of the iOS project template, then you should remove them, which has worked for us.

    Hope this helps.

    Paul Diston

  • PureKromePureKrome AUMember ✭✭

    Drats, that didn't work - because we don't have those lines in our file :(

    I feel like I haven't set up our first Page, properly? Previously, everything worked fine. But not now... ??

  • CraigDunnCraigDunn USXamarin Team Xamurai

    Xamarin.Forms takes care of this for you, as long as you have set the MainPage of your App class in its constructor (like this):

    public App ()
    {
        MainPage = new ContentPage { Title = "App Lifecycle Sample" }; // your page here
    }
    

    and your iOS AppDelegate looks like this:

    [Register ("AppDelegate")]
    public partial class AppDelegate :
        global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate // superclass new in 1.3
    {
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init ();
    
            LoadApplication (new App ());  // method is new in 1.3
    
            return base.FinishedLaunching (app, options);
        }
    }
    

    If you are doing a lot of processing in your FinishedLaunching or App constructor, you may be missing the 'startup timeout'... iOS requires apps to start within a certain amount of time or it kills them; remove any time-consuming logic from those two methods.

    @PureKrome you also said

    Previously, everything worked fine. But not now

    What changed? Did you update Xamarin? iOS? Xcode? Forms? What version of Xamarin.Forms are you using?

  • PureKromePureKrome AUMember ✭✭
    edited October 2015

    Hi Craig and thanks for the reply.

    I believe I'm now using iOS 9 to test this stuff out, in the simulator. I guess that means I auto-dl'd XCode to the latest v?

    Anyways, I have a this gist with my App.cs code. You were pretty much right => I wasn't setting my MainPage = in the ctor. I had another method which was doing this, in the OnStart method I overrode. Why there? Because my method is doing some async/await => await LoadAppDataAsync(); .. and you cannot have await in a ctor .. so I moved it to that method.

    Jumping into JabbR, i soon got some help from @pntkl and @jwood who suggested I move it to the ctor BUT use .ConfigureAwait(true);.

    eg. LoadAppDataAsync().ConfigureAwait(true);

    And that seemed to work :smile:

    So, I'm not sure why things worked before but not now (before I made the change, above) :smile:

  • BrandonPeckBrandonPeck USMember
    edited December 2015

    Solution above worked! @PureKrome thanks but I am actually having some trouble using the configureawait method. How did you use it to load async data in the constructor?

  • AlvinGeorge.ParuvananyAlvinGeorge.Paruvanany USMember ✭✭
    edited March 2016

    On AppDelegate :-

    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
            {
                // Override point for customization after application launch.
                // If not required for your application you can safely delete this method
    
                var indexVC = new IndexViewController ();
                window = new UIWindow ((CGRect)UIScreen.MainScreen.Bounds);
                window.RootViewController = indexVC;
                window.MakeKeyAndVisible ();
    
    
                return true;
            }
    

    Note: Please check info.plist and set the Main Interface to Launch Screen.

  • TobiasLeodolterTobiasLeodolter USMember

    Did you installed MonoTouch in your windows?
    Please make sure it is installed in your computer

  • MoorthyMoorthy USMember
    edited February 2017

    Hi,

    I am still having the problem, I couldn't able to solve it. Please can anyone help me. (I am using xcode version 8.2.1)

    This is my Main.cs

    public class Application
    {
    static void Main (string[] args)
    {
    UIApplication.Main(args, null, "AppDelegate");
    }
    }

    And this is my AppDelegate.cs

    [Register ("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
    {
    global::Xamarin.Forms.Forms.Init ();
    Xamarin.FormsMaps.Init();
    LoadApplication(new App());
    return base.FinishedLaunching(application, launchOptions);
    }
    }

  • lvnvlvnv USMember ✭✭
    edited July 2017

    I know this is an old thread. But I had the same problem recently when importing a new project.

    Setting the MainPage in App.cs fixed it for me

    MainPage = new ContentPage();

  • It could be the following issues :

    1. You forgot to register your View for navigation
    2. You forgot to register a type with the DI container
    3. You have a Null Reference or other Exception being thrown in your View or ViewModel. Note that the Exception could be thrown due to a custom control or other extension methods you are using in your View.
    4. There is an error with .xaml - try commenting out a section of the code
    5. Make sure you actually have a root page in your app.xaml.cs NavigationService.NavigateAsync("/HomePage");
    6. This function might help you find the real reason why its failing if it's failing in app.xaml.cs

      protected override async void OnInitialized()
      {
          try
          {
              TaskScheduler.UnobservedTaskException += (sender, e) => {
                  Logger.Log(e.Exception.ToString(), Category.Exception, Priority.High);
              };
              await NavigationService.NavigateAsync("HomePage");
          }
          catch(Exception e)
          {
              Logger.Log(e.Exception.ToString(), Category.Exception, Priority.High);
          }
      }
      
  • icalderondicalderond USMember ✭✭

    @ValentasMatusevicius Thanks
    This code work for me to show exactly my error.

Sign In or Register to comment.