【问题标题】:Xamarin Forms Android Error inflating class MaterialCalendarGridViewXamarin Forms Android 错误膨胀类 MaterialCalendarGridView
【发布时间】:2022-01-19 23:29:06
【问题描述】:

从 FormsApplicationActivity 迁移到 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'

错误发生在第一行base.OnCreate(savedInstanceState);的OnCreate中

我的主要活动是这样的

[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);
            }
        }

这是我的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>

这是 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"/>

更新

我可以通过删除layout.xml 文件并在styles.xml 文件中使用以下值来消除异常:

<?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>

知道如何在不删除 layout.xml 的情况下解决此问题吗?

更新 2 我的应用程序使用 NavigationPages。 我确实创建了一个新项目,但我找不到任何差异。

【问题讨论】:

  • 你也升级了Xamarin.Forms包吗?
  • @ColeX-MSFT 是的,所有软件包都在最新版本上。
  • 尝试删除解决方案中的所有 bin 和 obj 文件夹? (Windows 资源管理器,转到您的解决方案文件夹,搜索 :=bin。)
  • @ToolmakerSteve 并没有改变什么
  • 好的。 XForms 中一定发生了一些变化。我不确定是什么。您的应用程序是否使用“Shell”?标签页?导航页?创建一个新的 Xamarin Forms 项目。选择“空”模板。验证是否在 Android 上构建和运行。然后查看它的文件,看看你是否能从你的项目中发现一些不同之处。

标签: c# android xamarin.forms xamarin.android


【解决方案1】:

我能够修复异常,但现在我的工具栏完全消失了,我为这个问题创建了一个新的 stackoverflow 帖子:Xamarin Forms Android Toolbar missing after migration to Flyout and AppCompat

这里解决此问题的方法是删除layout.xml 文件并在styles.xml 文件中使用以下值:

<?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>

【讨论】:

  • @ToolmakerSteve 主要是你是对的,但我不知道为什么我的应用程序与 toolbar.xml 一起崩溃,我不知道如何修复它或进一步确定它为什么不工作。
  • 我明白了。好的,我会告诉你什么对获得帮助有帮助。我已将此信息添加到您问题末尾的新部分中。请对其进行编辑,然后粘贴您必须删除的 toolbar.xml 代码。然后删除这个答案 - 最好将其添加为问题的进展,就像我现在所做的那样。谢谢。
  • @ToolmakerSteve 没有toolbar.xml 抱歉,工具栏是在layout.xml 文件中定义的(内容有问题)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-08
  • 2021-04-09
  • 2016-11-12
  • 2016-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多