Brew the Mash

クラフトビール好きなITエンジニア@mashmorgan_jpの開発メモなど

【Android】1度開いたMapFragmentを再度開こうとするとInflateException

下タブをつけたAndroidアプリを作成しており、各タブはFragmentにしたのですが、MapFragmentを初回は表示できるのに別タブ選択後に再度表示しようとすると以下のエラーが発生。

android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #15: Duplicate id 0x7f0f00f3, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment

なんかfragmentの管理が出来てないっぽいけど解決方法がなかなか見つけられず。 すると英語ですがほぼ同じ現象のブログ(下部参考元)を発見!! (本当に有り難いです)

ポイントはViewをstaticで持たせるという点でした。

static View rootView;

viewをstatic化してnullチェックを入れるだけで無事に解決しました。

参考元: Android MapFragment exception when clicked twicecolinyeoh.wordpress.com