2

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.

WhiteIntel
  • 697
  • 1
  • 9
  • 24
  • Did you also upgrade `Xamarin.Forms` package ? – ColeX Dec 17 '21 at 06:31
  • @ColeX-MSFT yes, all packages are on the latest version. – WhiteIntel Dec 17 '21 at 11:37
  • Try deleting all bin and obj folders in your solution? (Windows explorer, go to your solution folder, search `:=bin`.) – ToolmakerSteve Dec 17 '21 at 15:22
  • @ToolmakerSteve did that changes nothing – WhiteIntel Dec 17 '21 at 19:00
  • 1
    Ok. Something must have changed in XForms. I'm not sure what. Does your app use "Shell"? TabbedPage? NavigationPage? Make a new Xamarin Forms project. Choose "empty" template. Verify that builds and runs on Android. Then look at its files, see if you can spot some difference from your project. – ToolmakerSteve Dec 17 '21 at 21:52

1 Answers1

0

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>
WhiteIntel
  • 697
  • 1
  • 9
  • 24
  • @ToolmakerSteve mainly you are right, but I have no idea why my application crashes with the toolbar.xml and I have no idea how to fix it or further identify why it is not working. – WhiteIntel Dec 17 '21 at 21:11
  • I understand. Okay, I'll show you what would be helpful in getting help. I've added this info into a new section at the end of your question. Please edit that, and paste in the toolbar.xml code that you had to remove. Then delete this answer - its better to add that as progress in the question, as I have now done. Thank you. – ToolmakerSteve Dec 17 '21 at 21:21
  • @ToolmakerSteve there was no toolbar.xml sorry, the toolbar was defined in the layout.xml file (content is in the question) – WhiteIntel Dec 17 '21 at 21:31