はじめに
本稿は、C#でのXamarin.Formsでの開発時に、Xamarin.Formsを古いバージョンからバージョンアップする時に、発生する実行時エラーの対処方法の記録です。
(少なくとも Xamarin.Forms の Ver4.3.0から5.0.0へのバージョンアップ時に発生することを確認済みです)
エラー内容
ビルドエラーはありませんが、Androidでの実行時に、以下のExceptionが発生します。
Exceptionの種類:Android.Views.InflateException
ExceptionのMessage:Binary XML file line #1: Binary XML file line #1: Error inflating class android.support.v7.widget.Toolbar
対処方法
以下のMicrosoftサイトのQ&Aにあるように、Toolbar.axmlというファイルの定義を修正することで解消できます。
https://docs.microsoft.com/en-us/answers/questions/186830/hemo-me-fix-this-androidviewsinflateexception.html
Toolbar.axmlとは、図の選択しているファイルのことです。
以下の変更前の行を、変更後の行に変更することで解消できます。
<android.support.v7.widget.Toolbar ←変更前
<androidx.appcompat.widget.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:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
また、以下のSyncfusion社でのQ&Aによると、Toolbar.axml だけでなく Tabbar.axml も変更するように書いてありました。
https://www.syncfusion.com/forums/155967/android-views-inflateexception-binary-xml-file-line-1-binary-xml-file-line-1-error
私の環境では、 Tabbar.axml は変更しなくても実行時エラーは発生しなくなりましたが、上記の記事の通りに、Tabbar.axml の以下の変更前の行を、変更後の行に修正した方が良さそうです。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android" ←変更前
<com.google.android.material.tabs.TabLayout xmlns:android="http://schemas.android.com/apk/res/android" ←変更後
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="@android:color/white"
app:tabGravity="fill"
app:tabMode="fixed" />