title: Xamarin.Forms.Android: java.lang.IllegalStateException: Fragment does not have a view
body: <p>I'm developing a Xamarin.Forms app. It works well, but keeps on crashing when the user brings it back from background after opening a few other apps. I'm new to Xamarin and I'm not sure how to debug this issue. This issue is not happening on iOS so far.</p>
  
  <p>SplashActivity.cs:</p>
  
  <pre><code>using System.Threading.Tasks;
  using Android.App;
  using Android.Content;
  using Android.OS;
  using Android.Support.V7.App;
  using Android.Util;
  
  namespace MyApp.Droid
  {
      [Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
      public class SplashActivity : AppCompatActivity
      {
          static readonly string TAG = "X:" + typeof(SplashActivity).Name;
  
          protected override void OnCreate(Bundle savedInstanceState) //  PersistableBundle persistentState
          {
              base.OnCreate(savedInstanceState); // persistentState
              Log.Debug(TAG, "SplashActivity.OnCreate");
          }
  
          protected override void OnResume()
          {
              base.OnResume();
  
              Task startupWork = new Task(() =&gt;
                                          {
                                              Log.Debug(TAG, "Performing some startup work that takes a bit of time.");
                                              Task.Delay(5000); // Simulate a bit of startup work.
                                              Log.Debug(TAG, "Working in the background - important stuff.");
                                          });
  
              startupWork.ContinueWith(t =&gt;
                                       {
                                           Log.Debug(TAG, "Work is finished - start Activity1.");
                                           StartActivity(new Intent(Application.Context, typeof(MainActivity)));
                                       }, TaskScheduler.FromCurrentSynchronizationContext());
  
              startupWork.Start();
          }
      }
  }
  </code></pre>
  
  <p>MainActivity.cs:</p>
  
  <pre><code>using System;
  
  using Android.App;
  using Android.Content;
  using Android.Content.PM;
  using Android.Runtime;
  using Android.Views;
  using Android.Widget;
  using Android.OS;
  
  using HockeyApp.Android;
  using HockeyApp.Android.Metrics;
  
  using Xamarin.Android.Net; // Keep to make TLS 1.2 work
  
  namespace MyApp.Droid
  {
      [Activity(Label = "EventingVolunteers.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
      public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
      {
          protected override void OnCreate(Bundle savedInstanceState)
          {
              TabLayoutResource = Resource.Layout.Tabbar;
              ToolbarResource = Resource.Layout.Toolbar;
  
              base.OnCreate(savedInstanceState);
  
              global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
  
              LoadApplication(new App());
  
              // HockeyApp
              CrashManager.Register(this, Constants.HockeyAppID_Android);
              MetricsManager.Register(this, Application, Constants.HockeyAppID_Android);
  
              // Push Notifications
              RegisterForGCM();
          }
  
          private void RegisterForGCM()
          {
              string senders = Constants.GoogleConsoleProjectId;
              Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
              intent.SetPackage("com.google.android.gsf");
              intent.PutExtra("app", PendingIntent.GetBroadcast(this, 0, new Intent(), 0));
              intent.PutExtra("sender", senders);
              StartService(intent);
          }
  
          protected override void OnPause()
          {
              base.OnPause();
          }
  
          protected override void OnResume()
          {
              base.OnResume();
          }
  
          protected override void OnDestroy()
          {
              base.OnDestroy();
          }
      }
  }
  </code></pre>
  
  <p>Error message:</p>
  
  <pre><code>java.lang.IllegalStateException: Fragment does not have a view
  android.support.v4.app.Fragment$1.onFindViewById()Fragment.java:1933
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1057
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1252
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1234
  android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated()FragmentManager.java:2046
  android.support.v4.app.Fragment.performActivityCreated()Fragment.java:1989
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1092
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1252
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1234
  android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated()FragmentManager.java:2046
  android.support.v4.app.FragmentController.dispatchActivityCreated()FragmentController.java:174
  android.support.v4.app.FragmentActivity.onStart()FragmentActivity.java:598
  md5b60ffeb829f638581ab2bb9b1a7f4f3f.FormsAppCompatActivity.n_onStart(Native Method)
  md5b60ffeb829f638581ab2bb9b1a7f4f3f.FormsAppCompatActivity.onStart()FormsAppCompatActivity.java:120
  android.app.Instrumentation.callActivityOnStart()Instrumentation.java:1260
  android.app.Activity.performStart()Activity.java:6261
  android.app.ActivityThread.performLaunchActivity()ActivityThread.java:2389
  android.app.ActivityThread.handleLaunchActivity()ActivityThread.java:2490
  at android.app.ActivityThread.-wrap11(ActivityThread.java)
  android.app.ActivityThread$H.handleMessage()ActivityThread.java:1354
  android.os.Handler.dispatchMessage()Handler.java:102
  android.os.Looper.loop()Looper.java:148
  android.app.ActivityThread.main()ActivityThread.java:5456
  java.lang.reflect.Method.invoke(Native Method)
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run()ZygoteInit.java:728
  com.android.internal.os.ZygoteInit.main()ZygoteInit.java:618
  Xamarin caused by: Java.Lang.IllegalStateException: Fragment does not have a view
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualVoidMethod(JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args)
  Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(string encodedMember, IJavaPeerable self, JniArgumentValue* parameters)
  Android.App.Activity.OnStart()
  Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnStart()
  Android.App.Activity.n_OnStart(IntPtr jnienv, IntPtr native__this)
  at (wrapper dynamic-method) System.Object:23d8bc20-60cf-4cac-a339-6cc6722e8667 (intptr,intptr)
  --- End of managed Java.Lang.IllegalStateException stack trace ---
  java.lang.IllegalStateException: Fragment does not have a view
  android.support.v4.app.Fragment$1.onFindViewById()Fragment.java:1933
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1057
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1252
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1234
  android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated()FragmentManager.java:2046
  android.support.v4.app.Fragment.performActivityCreated()Fragment.java:1989
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1092
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1252
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1234
  android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated()FragmentManager.java:2046
  android.support.v4.app.FragmentController.dispatchActivityCreated()FragmentController.java:174
  android.support.v4.app.FragmentActivity.onStart()FragmentActivity.java:598
  md5b60ffeb829f638581ab2bb9b1a7f4f3f.FormsAppCompatActivity.n_onStart(Native Method)
  md5b60ffeb829f638581ab2bb9b1a7f4f3f.FormsAppCompatActivity.onStart()FormsAppCompatActivity.java:120
  android.app.Instrumentation.callActivityOnStart()Instrumentation.java:1260
  android.app.Activity.performStart()Activity.java:6261
  android.app.ActivityThread.performLaunchActivity()ActivityThread.java:2389
  android.app.ActivityThread.handleLaunchActivity()ActivityThread.java:2490
  at android.app.ActivityThread.-wrap11(ActivityThread.java)
  android.app.ActivityThread$H.handleMessage()ActivityThread.java:1354
  android.os.Handler.dispatchMessage()Handler.java:102
  android.os.Looper.loop()Looper.java:148
  android.app.ActivityThread.main()ActivityThread.java:5456
  java.lang.reflect.Method.invoke(Native Method)
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run()ZygoteInit.java:728
  com.android.internal.os.ZygoteInit.main()ZygoteInit.java:618
  --- End of managed Java.Lang.IllegalStateException stack trace ---
  java.lang.IllegalStateException: Fragment does not have a view
  android.support.v4.app.Fragment$1.onFindViewById()Fragment.java:1933
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1057
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1252
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1234
  android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated()FragmentManager.java:2046
  android.support.v4.app.Fragment.performActivityCreated()Fragment.java:1989
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1092
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1252
  android.support.v4.app.FragmentManagerImpl.moveToState()FragmentManager.java:1234
  android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated()FragmentManager.java:2046
  android.support.v4.app.FragmentController.dispatchActivityCreated()FragmentController.java:174
  android.support.v4.app.FragmentActivity.onStart()FragmentActivity.java:598
  md5b60ffeb829f638581ab2bb9b1a7f4f3f.FormsAppCompatActivity.n_onStart(Native Method)
  md5b60ffeb829f638581ab2bb9b1a7f4f3f.FormsAppCompatActivity.onStart()FormsAppCompatActivity.java:120
  android.app.Instrumentation.callActivityOnStart()Instrumentation.java:1260
  android.app.Activity.performStart()Activity.java:6261
  android.app.ActivityThread.performLaunchActivity()ActivityThread.java:2389
  android.app.ActivityThread.handleLaunchActivity()ActivityThread.java:2490
  at android.app.ActivityThread.-wrap11(ActivityThread.java)
  android.app.ActivityThread$H.handleMessage()ActivityThread.java:1354
  android.os.Handler.dispatchMessage()Handler.java:102
  android.os.Looper.loop()Looper.java:148
  android.app.ActivityThread.main()ActivityThread.java:5456
  java.lang.reflect.Method.invoke(Native Method)
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run()ZygoteInit.java:728
  com.android.internal.os.ZygoteInit.main()ZygoteInit.java:618
  </code></pre>
  
  <p>App.xaml.cs:</p>
  
  <pre><code>using System;
  using System.Linq;
  using Amazon;
  using Xamarin.Forms;
  
  namespace MyApp
  {
      public partial class App : Application
      {
          public static string AppName { get { return "MyApp"; } }
  
          public static User User { get; set; }
  
          public static UserManager UserManager { get; private set; }
  
          public static VolunteerManager VolunteerManager { get; private set; }
  
          public static SignupManager SignupManager { get; private set; }
  
          static NavigationPage NavPage;
  
          public static bool IsLoggedIn
          {
              get
              {
                  if (User != null)
                      return !string.IsNullOrWhiteSpace(User.Email);
                  else
                      return false;
              }
          }
  
          public static Action SuccessfulLoginAction
          {
              get
              {
                  return new Action(() =&gt;
                  {
                      NavPage.Navigation.PopModalAsync();
  
                      if (IsLoggedIn)
                      {
                          // Update RestService with new token
                          UserManager = new UserManager(new RestService());
                          VolunteerManager = new VolunteerManager(new RestService());
                          SignupManager = new SignupManager(new RestService());
  
                          NavPage.Navigation.InsertPageBefore(new VolunteerDashboardPage(), NavPage.Navigation.NavigationStack.First());
                          NavPage.Navigation.PopToRootAsync();
                      }
                  });
              }
          }
  
          public static Action LogoutAction
          {
              get
              {
                  return new Action(() =&gt;
                  {
                      DependencyService.Get&lt;ICredentialsService&gt;().DeleteCredentials();
                      App.User = null;
                      // NavPage.Navigation.PopModalAsync();
                      NavPage.Navigation.InsertPageBefore(new LoginPage(), NavPage.Navigation.NavigationStack.First());
                      NavPage.Navigation.PopToRootAsync();
                  });
              }
          }
  
          public App()
          {
              User = new User();
              UserManager = new UserManager(new RestService());
              VolunteerManager = new VolunteerManager(new RestService());
              SignupManager = new SignupManager(new RestService());
  
              // AWS SDK
              var loggingConfig = AWSConfigs.LoggingConfig;
              loggingConfig.LogMetrics = true;
              loggingConfig.LogResponses = ResponseLoggingOption.Always;
              loggingConfig.LogMetricsFormat = LogMetricsFormatOption.JSON;
              loggingConfig.LogTo = LoggingOptions.SystemDiagnostics;
  
              //AWSConfigs.AWSRegion = "us-east-1";
  
              // Check if login needed
              if (DependencyService.Get&lt;ICredentialsService&gt;().DoCredentialsExist())
              {
                  NavPage = new NavigationPage(new VolunteerDashboardPage());
                  MainPage = NavPage;
                  // MainPage = new VolunteerDashboardPage();
              } else {
                  NavPage = new NavigationPage(new LoginPage());
                  MainPage = NavPage;
              }
          }
  
          protected override void OnStart()
          {
              // Handle when your app starts
          }
  
          protected override void OnSleep()
          {
              // Handle when your app sleeps
          }
  
          protected override void OnResume()
          {
              // Handle when your app resumes
          }
      }
  }
  </code></pre>