💡

BottomNavigationViewで新しく画面を追加したときにエラーが出た

2023/02/19に公開

状況

新しくFragmentを作って、ボトムナビゲーションに追加したら、アプリを起動した瞬間落ちる。

エラー内容

E  FATAL EXCEPTION: main
Process: com.example.hideandseek, PID: 19308
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hideandseek/com.example.hideandseek.ui.view.MainActivity}: android.view.InflateException: Binary XML file line #17 in com.example.hideandseek:layout/activity_main: Binary XML file line #17 in com.example.hideandseek:layout/activity_main: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4035)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4201)
	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2438)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loopOnce(Looper.java:226)
	at android.os.Looper.loop(Looper.java:313)
	at android.app.ActivityThread.main(ActivityThread.java:8669)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
Caused by: android.view.InflateException: Binary XML file line #17 in com.example.hideandseek:layout/activity_main: Binary XML file line #17 in com.example.hideandseek:layout/activity_main: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
Caused by: android.view.InflateException: Binary XML file line #17 in com.example.hideandseek:layout/activity_main: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
Caused by: java.lang.reflect.InvocationTargetException
	at java.lang.reflect.Constructor.newInstance0(Native Method)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
	at android.view.LayoutInflater.createView(LayoutInflater.java:858)
	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1010)
	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
	at android.view.LayoutInflater.rInflate(LayoutInflater.java:1127)
	at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
	at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
	at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
	at com.example.hideandseek.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:45)
	at com.example.hideandseek.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:39)
	at com.example.hideandseek.ui.view.MainActivity.onCreate(MainActivity.kt:55)
	at android.app.Activity.performCreate(Activity.java:8290)
	at android.app.Activity.performCreate(Activity.java:8270)
	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4009)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4201)
	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2438)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loopOnce(Looper.java:226)
	at android.os.Looper.loop(Looper.java:313)
	at android.app.ActivityThread.main(ActivityThread.java:8669)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

エラー文を見るとError inflating class com.google.android.material.bottomnavigation.BottomNavigationViewとあるように、BottomNavigationViewが原因みたい

これだけだと、具体的な解決法が分からないので、BottomNavigationViewについて調べてみる

原因

https://m3.material.io/components/navigation-bar/guidelines
上記サイトのGuidelines > Usageで

Navigation bars (nav bars) provide access to three to five destinations.

という記述があった。
BottomNavigationViewで扱えるitemの数は3~5個までらしい。

解決策

よって、解決策としては画面数を減らすこと。(根本的な解決法ではないが)
もしくは、別の方法で画面遷移をすること。

私は、必ずしもボトムナビが使いたいわけではなかったので、navigationGraphで対応することにしました。

Discussion