question

pierrelouisdeschamps-1678 avatar image
0 Votes"
pierrelouisdeschamps-1678 asked JarvanZhang-MSFT edited

Android.Views.InflateException Binary XML file line #1 in layout/toolbar: Error inflating class androidx.appcompat.widget.Toolbar

In a xamarin.forms android project, MainActivity.cs contains:

   [Activity (Label = "Almicantarat", Theme = "@style/MainTheme",
   Icon = "@drawable/almicantarat_launcher", 
   MainLauncher = true,
   ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
   public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
   {

I need to remove:

Theme = "@style/MainTheme",

in order to use a custom theme and a toolbar.

But then I met the following exception:

Android.Views.InflateException
Message=Binary XML file line #1 in almicantarat.almicantarat:layout/toolbar: Binary XML file line #1 in almicantarat.almicantarat:layout/toolbar: Error inflating class androidx.appcompat.widget.Toolbar

This is styles.xml:

 <?xml version="1.0" encoding="utf-8" ?>
 <resources>
   <style name="AlmicantaratTheme" parent="@android:Theme.Material">
       <item name="android:windowNoTitle">true</item>
       <item name="android:windowActionBar">false</item>
       <item name="android:colorPrimary">#15014E</item>
       <item name="android:colorPrimaryDark">#15014E</item>
       <item name="android:navigationBarColor">#15014E</item>
       <item name="android:statusBarColor">#15014E</item>
       <item name="android:colorAccent">#FF4081</item>
       <item name="android:windowActionModeOverlay">true</item>
   </style>
 </resources>

This is Toolbar.axml:

 <androidx.appcompat.widget.Toolbar
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/toolbar"
     android:layout_width="match_parent"
     android:layout_height="?attr/actionBarSize"
     android:minHeight="?attr/actionBarSize"
     android:background="?attr/colorPrimary"
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
     app:layout_scrollFlags="scroll|enterAlways" />

How can I solve that?

hint: this is an long time running project started in 2014. It migrated successfully to AndroidX.
It targets SDK 29 and requires minimum SDK 21.

using VS2019
Xamarin.Forms v5.0.0.2125





dotnet-xamarinformsdotnet-android
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered

Hello pierrelouisdeschamps-1678,​

Welcome to our Microsoft Q&A platform!

Error inflating class androidx.appcompat.widget.Toolbar

I tested the code and reproduced the issue on my side. This is because @android:Theme.Material theme is not applicable in AndroidX. Please using Theme.AppCompat.* instead. Such as:

<style name="AlmicantaratTheme" parent="Theme.AppCompat.NoActionBar">
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowActionBar">false</item>
  <item name="android:colorPrimary">#15014E</item>
  <item name="android:colorPrimaryDark">#15014E</item>
  <item name="android:navigationBarColor">#15014E</item>
  <item name="android:statusBarColor">#15014E</item>
  <item name="android:colorAccent">#FF4081</item>
  <item name="android:windowActionModeOverlay">true</item>
</style>

Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

pierrelouisdeschamps-1678 avatar image
0 Votes"
pierrelouisdeschamps-1678 answered JarvanZhang-MSFT edited

Hello @JarvanZhang-MSFT
Then I get the following build error:

resource android:style/Theme.AppCompat.NoActionBar not found.

What nuget package should I install?
I have already installed the following packages:

 sqlite-net-pcl                      {1.8.116}      Almicantarat.Droid                                                         
 SQLitePCLRaw.bundle_green           {2.0.6}        Almicantarat.Droid                                                         
 SQLitePCLRaw.lib.e_sqlite3          {2.0.6}        Almicantarat.Droid                                                         
 SQLitePCLRaw.provider.e_sqlite3.... {1.1.14}       Almicantarat.Droid                                                         
 System.Memory                       {4.5.4}        Almicantarat.Droid                                                         
 System.Numerics.Vectors             {4.5.0}        Almicantarat.Droid                                                         
 System.Runtime.CompilerServices.... {5.0.0}        Almicantarat.Droid                                                         
 UrhoSharp.Forms                     {1.9.67}       Almicantarat.Droid                                                         
 Xamarin.AndroidX.Legacy.Support.... {1.0.0.8}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.Browser            {1.3.0.6}      Almicantarat.Droid                                                         
 Xamarin.Google.Android.Material     {1.4.0.2}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.MediaRouter        {1.2.5}        Almicantarat.Droid                                                         
 Xamarin.AndroidX.AppCompat          {1.3.1.1}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.AppCompat.AppCo... {1.3.1.1}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.AsyncLayoutInfl... {1.0.0.8}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.CardView           {1.0.0.9}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.CursorAdapter      {1.0.0.8}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.Legacy.Support.V4  {1.0.0.8}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.Lifecycle.LiveData {2.3.1.1}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.Media              {1.4.2}        Almicantarat.Droid                                                         
 Xamarin.AndroidX.MultiDex           {2.0.1.8}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.Palette            {1.0.0.8}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.RecyclerView       {1.2.1.1}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.SwipeRefreshLayout {1.1.0.3}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.Transition         {1.4.1.1}      Almicantarat.Droid                                                         
 Xamarin.Essentials                  {1.7.0}        Almicantarat.Droid                                                         
 Xamarin.Forms                       {5.0.0.2125}   Almicantarat.Droid                                                         
 Xamarin.Forms.Maps                  {5.0.0.2125}   Almicantarat.Droid                                                         
 Xamarin.GooglePlayServices.Base     {117.6.0}      Almicantarat.Droid                                                         
 Xamarin.GooglePlayServices.Maps     {117.0.1}      Almicantarat.Droid                                                         
 Xamarin.AndroidX.Migration          {1.0.8}        Almicantarat.Droid

The previous Xamarin.Android.Support.* packages had been uninstalled by the AndroidX migration tool.

regards,

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Done!
I got the following build error:
resource android:style/Theme.AppCompat.NoActionBar not found.

because I wrote:
<style name="AlmicantaratTheme" parent="@android:Theme.AppCompat.NoActionBar">

instead of:
<style name="AlmicantaratTheme" parent="Theme.AppCompat.NoActionBar">


Now it compiles!

Thanks

1 Vote 1 ·
JarvanZhang-MSFT avatar image JarvanZhang-MSFT pierrelouisdeschamps-1678 ·

My pleasure. Happy coding!

0 Votes 0 ·