Search code examples
c#androidxamarin.formsxamarin.android

Xamarin Forms Android Error inflating class MaterialCalendarGridView


I get the following Error message after migrating from FormsApplicationActivity to FormsAppCompatActivity:

Android.Views.InflateException: 'Binary XML file line #31 in com.myapp.app:layout/mtrl_calendar_month_labeled: Binary XML file line #18 in com.myapp.app:layout/mtrl_calendar_month: Error inflating class com.google.android.material.datepicker.MaterialCalendarGridView'

The error happens in the OnCreate at the first line base.OnCreate(savedInstanceState);

My mainactivity looks like this

[Activity(ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait, LaunchMode = LaunchMode.SingleTop, MainLauncher = true, Theme = "@style/MyAppTheme")]
    [IntentFilter(new[] { Intent.ActionView},
        DataScheme = "https",
        DataHost =  "MyApp.com",
        AutoVerify = true,
        Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable})]
    [IntentFilter(new[] { Intent.ActionView },
        DataScheme = "http",
        DataHost = "MyApp.com",
        AutoVerify = true,
        Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable })]


    //[Activity(Label = "Muzzillo_v1", MainLauncher = true, Theme = "@style/AppTheme")]
    public class MainActivity : FormsAppCompatActivity, Android.Gms.Tasks.IOnSuccessListener
    {
        static MainActivity instance = null;
        public const string TAG = "MainActivity";
        internal static readonly string CHANNEL_ID = "my_notification_channel";


        public static Context AppContext;

        public static MainActivity CurrentActivity { get { return instance; } }

        IDeviceInstallationService _deviceInstallationService;
        IDeviceInstallationService DeviceInstallationService
    => _deviceInstallationService ??
        (_deviceInstallationService =
        DependencyService.Resolve<IDeviceInstallationService>());

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

            AppCenter.Start(Constants.AppCenterAndroidKey,
                       typeof(Analytics), typeof(Crashes));

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);

            if (Intent.Extras != null)
            {
                foreach (var key in Intent.Extras.KeySet())
                {
                    if (key != null)
                    {
                        var value = Intent.Extras.GetString(key);
                        Log.Debug(TAG, "Key: {0} Value: {1}", key, value);
                    }
                }
            }

            AppContext = this;
            instance = this;

            DependencyService.Register<IFileSystemAccessService, FileSystemAccessService>();
            DependencyService.Register<IDevicePropertyService, DevicePropertyService>();
            DependencyService.RegisterSingleton<IDeviceInstallationService>(new DeviceInstallationService());

            MR.Gestures.Android.Settings.LicenseKey = "P3CL-CRL5-YJFM-KJ6P-DRFT-DK4N-WRCL-XYP5-JY7Z-92HB-CWQM-5LQA-JNVS";

            if (DeviceInstallationService.NotificationsSupported)
            {
                FirebaseInstanceId.GetInstance(Firebase.FirebaseApp.Instance)
                    .GetInstanceId()
                    .AddOnSuccessListener(this);
            }

            LoadApplication(new App());

            App.Current.NotificationsUpdated += Current_NotificationsUpdated;

            ProcessNotificationActions(Intent);

            try
            {
                System.Diagnostics.Debug.WriteLine("Registering for push notifications ...");
                IsPlayServicesAvailable();
                CreateNotificationChannel();
            }
            catch (Exception ex)
            {
                Logger.Critical(ex);
                System.Diagnostics.Debug.WriteLine("MainActivity :: Error :: {0}", ex.Message);
            }
        }

This is my styles.xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="MyAppTheme" parent="Theme.AppCompat.Light">
        <item name="android:colorPrimary">#ec1a23</item>
        <item name="android:colorPrimaryDark">#dd2c00</item>
        <item name="android:colorAccent">#ff3d00</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
  </style>
    <style name="Theme.Splash" parent="android:Theme">
        <item name="android:windowBackground">@drawable/splashscreen</item>
        <item name="android:windowNoTitle">true</item>
    </style>
    <style name="MyTheme" parent="MyTheme.Base">
    </style>
    <style name="MyTheme.Base" parent="android:Theme.Holo.Light">
        <item name="android:colorPrimary">@color/primary</item>
        <item name="android:colorPrimaryDark">@color/primaryDark</item>
        <item name="android:colorAccent">@color/accent</item>
        <item name="android:colorActivatedHighlight">@color/highlight</item>
        <item name="android:actionMenuTextColor">@color/actionbar_text</item>
    </style>
</resources>

And this the layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:minHeight="?android:attr/actionBarSize"
    android:background="?android:attr/colorPrimary"/>

UPDATE

I can make the exception go away, by deleting the layout.xml file and using the following values in the styles.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="MyAppTheme" parent="Theme.MaterialComponents.Light">
        <item name="android:colorPrimary">#ec1a23</item>
        <item name="android:colorPrimaryDark">#dd2c00</item>
        <item name="android:colorAccent">#ff3d00</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
  </style>
    <style name="Theme.Splash" parent="Theme.MaterialComponents.Light">
        <item name="android:windowBackground">@drawable/splashscreen</item>
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>

Any idea how to fix this, without removing layout.xml?

UPDATE 2 My App uses NavigationPages. I did created a new Project, I cant find any differences.


Solution

  • I was able to fix the exception, but now my toolbar is completely gone and I created a new stackoverflow post for that problem: Xamarin Forms Android Toolbar missing after migration to Flyout and AppCompat

    The solution for this problem here was to delete the layout.xml file and to use the following values in the styles.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <resources>
        <style name="MyAppTheme" parent="Theme.MaterialComponents.Light">
            <item name="android:colorPrimary">#ec1a23</item>
            <item name="android:colorPrimaryDark">#dd2c00</item>
            <item name="android:colorAccent">#ff3d00</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
      </style>
        <style name="Theme.Splash" parent="Theme.MaterialComponents.Light">
            <item name="android:windowBackground">@drawable/splashscreen</item>
            <item name="android:windowNoTitle">true</item>
        </style>
    </resources>