質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

1673閲覧

Android Navigationについて

_kei

総合スコア26

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2021/08/24 15:03

前提・実現したいこと

BottomNavigationView と Navigation を併用したいです。なぜかエラーが出力され、原因も不明であるため、どこがまちがっているか教えていただきたいです。

発生している問題・エラーメッセージ

Unable to start activity ComponentInfo{com.example.hoge/com.example.hoge.ui.MainActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class fragment

具体的な原因が不明であるため、解決できません。

該当のソースコード

MainActivity.kt

kotlin

1class MainActivity : AppCompatActivity() { 2 3 private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) } 4 5 override fun onCreate(savedInstanceState: Bundle?) { 6 super.onCreate(savedInstanceState) 7 setContentView(R.layout.activity_main) 8 9 binding.run{ 10 navigation.setupWithNavController( 11 Navigation.findNavController( 12 this@MainActivity, 13 R.id.nav_fragment 14 ) 15 ) 16 } 17 } 18}

MainActivity.ktのレイアウト

kotlin

1<?xml version="1.0" encoding="utf-8"?> 2<layout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools"> 5 6 <androidx.constraintlayout.widget.ConstraintLayout 7 android:id="@+id/container" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 tools:context=".presentation.screen.home.MainActivity"> 11 12 <fragment 13 android:id="@+id/nav_fragment" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:name="androidx.navigation.fragment.NavHostFragment" 17 app:navGraph="@navigation/navigation_graph" 18 app:defaultNavHost="true" 19 app:layout_constraintStart_toStartOf="parent" 20 app:layout_constraintEnd_toEndOf="parent" 21 app:layout_constraintTop_toTopOf="parent" 22 app:layout_constraintBottom_toBottomOf="@id/navigation"/> 23 24 <com.google.android.material.bottomnavigation.BottomNavigationView 25 android:id="@+id/navigation" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:background="?android:attr/windowBackground" 29 app:layout_constraintBottom_toBottomOf="parent" 30 app:layout_constraintLeft_toLeftOf="parent" 31 app:layout_constraintRight_toRightOf="parent" 32 app:itemTextColor="@color/black" 33 app:itemIconTint="@color/white" 34 app:menu="@menu/bottom_menu"/> 35 36 </androidx.constraintlayout.widget.ConstraintLayout> 37</layout>

menuフォルダに入っているファイル(bottom_menu.xml)

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/fugaFragment" android:icon="@drawable/ic_huga" android:title="fuga" /> <item android:id="@+id/hogeFragment" android:icon="@drawable/ic_hoge" android:title="hoge" /> </menu>

navigationフォルダに入っているnavigation_gragh.xml

<navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/bottom_navigation_view" app:startDestination="@id/fugaFragment"> <fragment android:id="@+id/fugaFragment" android:name="com.example.hoge.ui.FugaFragment" android:label="FugaFragment" /> <fragment android:id="@+id/hogeFragment" android:name="com.example.hoge.ui.HogeFragment" android:label="HogeFragment"/> </navigation>

2つのFragment

kotlin

1class XxxFragment : Fragment() { 2 override fun onCreateView( 3 inflater: LayoutInflater, container: ViewGroup?, 4 savedInstanceState: Bundle? 5 ): View? { 6 return inflater.inflate(R.layout.fragment_xxx, container, false) 7 } 8}

Fragmentのレイアウト

<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> </data> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ui.XxxFragment"> <TextView android:id="@+id/xxx" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:text="xxx" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout>

実装するうえで読んだ記事、エラーを解決するために読んだ記事

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2021/08/24 15:16 編集

コードに package 文があれば、それも含めてください。 > 2つのFragment コードが一つしかありませんが、「2つ」とは?
_kei

2021/08/24 15:33

コメントありがとうございます。 xxxが異なるだけで、それ以外の内容は全く同じであるため、1つのコードしか載せていません。 package文とはなんでしょうか?importしている部分のことでしょうか?その場合、実行は出来ているため不要だと思ったのですが、、。
jimbe

2021/08/24 15:42

いえ、package が無ければ結構です。 フラグメントの件は了解致しました。 宜しければ「xxxが異なるだけで~」と追加しておいて頂けるとありがたいです。 それで、エラーメッセージを御覧になって「具体的な原因が不明」と言われるのは、英語が分からなかったということでしょうか。
_kei

2021/08/24 15:46

いえ、エラーメッセージを読み、どこのコードが間違っているのか特定することができなかったということです。
jimbe

2021/08/24 17:34 編集

ではまず、「発生している問題・エラーメッセージ」には一行だけ書かれていますが「実装するうえで読んだ記事、エラーを解決するために読んだ記事」にある stackoverflow のリンク先では java.lang.RuntimeException: から始まり多数の行が出力されています。この件でも同じように多数出ていませんでしょうか。 ありましたら「発生している問題・エラーメッセージ」の所に全て張り付けて頂けませんか。
hoshi-takanori

2021/08/24 23:05 編集

Data Binding を使ってるのに setContentView してるからでは。 あと、エラーメッセージというか、スタックトレースを貼る時は、ちょっと長いですが全体を張ってくれると助かります。 具体的には、下の方に Caused by: java.lang.IllegalArgumentException: Binary XML file line #xx: Duplicate id 0xxxxx, tag null, or parent id 0xffffffff with another fragment for ... みたいなのが出てるはず。
_kei

2021/08/25 13:17

hoshi-takanori 様  コメントしていただき、ありがとうございます。無事解決することができました。回答を作成していただければ、ベストアンサーを差し上げますのでよろしくお願い致します。
guest

回答1

0

自己解決

MainActivity.kt において、onCreateメソッドの中で、作成されたBindinクラスのrootをsetContentViewに適用すれば解決することができました。

投稿2021/08/25 13:22

_kei

総合スコア26

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問