Xamarin Forms Android 膨胀 class MaterialCalendarGridView 时出错
Xamarin Forms Android Error inflating class MaterialCalendarGridView
从 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'
错误发生在第一行的OnCreate base.OnCreate(savedInstanceState);
我的主要活动如下所示
[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。
我确实创建了一个新项目,我找不到任何不同之处。
我能够修复异常,但现在我的工具栏完全消失了,我为该问题创建了一个新的 Whosebug post: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>
从 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'
错误发生在第一行的OnCreate base.OnCreate(savedInstanceState);
我的主要活动如下所示
[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。 我确实创建了一个新项目,我找不到任何不同之处。
我能够修复异常,但现在我的工具栏完全消失了,我为该问题创建了一个新的 Whosebug post: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>