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

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

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

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Android

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

Kotlin

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

Q&A

解決済

1回答

367閲覧

KotlinでFragmentの画面遷移が出来ません。

Yakusugi

総合スコア123

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Android

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

Kotlin

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

0グッド

0クリップ

投稿2023/02/09 08:50

編集2023/02/11 11:40

Kotlin(xml)でAndroidアプリを作成しています。
MainActivity上に4つのFragmentを用意したのですが、
BottomNavigationのアイコンをクリックしてもぞれぞれのFragmentに遷移しません。
原因が掴めておらず、もし下記コードに不備などがありましたら、ご指摘頂ければと思います。
※また、特にエラーも発生しておりません。。。

MainActivity.kt

1import androidx.appcompat.app.AppCompatActivity 2import android.os.Bundle 3import androidx.fragment.app.Fragment 4import com.undeniabledreams.cloudbudgettracker.R 5import com.undeniabledreams.cloudbudgettracker.databinding.ActivityMainBinding 6 7class MainActivity : AppCompatActivity() { 8 9 private lateinit var binding : ActivityMainBinding 10 11 override fun onCreate(savedInstanceState: Bundle?) { 12 super.onCreate(savedInstanceState) 13 binding = ActivityMainBinding.inflate(layoutInflater) 14 setContentView(binding.root) 15 replaceFragment(DashFragment()) 16 17 binding.bottomNavView.setOnItemSelectedListener { 18 19 when(it.itemId) { 20 R.id.dash -> replaceFragment(DashFragment()) 21 R.id.stats -> replaceFragment(StatsFragment()) 22 R.id.search -> replaceFragment(SearchFragment()) 23 R.id.convert -> replaceFragment(ConverterFragment()) 24 25 else -> { 26 27 } 28 } 29 true 30 } 31 32 } 33 34 private fun replaceFragment(fragment: Fragment) { 35 val fragmentManager = supportFragmentManager 36 val fragmentTransaction = fragmentManager.beginTransaction() 37 fragmentTransaction.replace(R.id.frame_layout, fragment) 38 fragmentTransaction.commit() 39 } 40 41}

activity_main.xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout 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 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".view.MainActivity"> 8 9 10 <FrameLayout 11 android:id="@+id/frame_layout" 12 android:layout_width="0dp" 13 android:layout_height="0dp" 14 app:layout_constraintEnd_toEndOf="parent" 15 app:layout_constraintStart_toStartOf="parent" 16 app:layout_constraintTop_toTopOf="parent"> 17 18 </FrameLayout> 19 20 <com.google.android.material.bottomnavigation.BottomNavigationView 21 android:id="@+id/bottom_nav_view" 22 android:layout_width="0dp" 23 android:layout_height="wrap_content" 24 app:layout_constraintBottom_toBottomOf="parent" 25 app:layout_constraintEnd_toEndOf="parent" 26 app:layout_constraintStart_toStartOf="parent" 27 app:menu="@menu/bottom_nav" 28 /> 29</androidx.constraintlayout.widget.ConstraintLayout>

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

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

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

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

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

jimbe

2023/02/09 09:26

レイアウト xml はどうなっているのでしょうか。
Yakusugi

2023/02/09 09:34

コメントありがとうございます。 activity_main.xmlを追加しました。
jimbe

2023/02/09 10:42 編集

編集ありがとうございます。 レイアウト xml 内の frame_layout の レイアウト制約に Bottom がありません。 layout_height も 0dp ですので、表示が更新されても見えていないのではないでしょうか。
Yakusugi

2023/02/09 10:47

ご指摘ありがとうございます。 上記の様に修正することで解決しました。
jimbe

2023/02/09 10:54 編集

>app:layout_constraintBaseline_toTopOf 書くなら app:layout_constraintBottom_toTopOf かと思います。 そして、 app:layout_constraintBottom_toTopOf によってビューの大きさが求められるなら、 android:layout_height は 0dp のままで構いません。 逆に android:layout_height="match_parent" を書くなら app:layout_constraintBottom_~ は無くても構いません。 また、質問のコードを修正してしまうと、何が問題だったのかが分かり難くなります。 修正の内容は回答として書いて頂いたほうが良いと思います。
Yakusugi

2023/02/11 11:41

返信遅くなりすみません。 確かに仰る通り、質問のコードを修正してしまうと、何が問題だったのかが分かり難くなりますね。。。 質問のコードは修正前に直し、修正後のコードは回答に書きました。
jimbe

2023/02/11 13:47

ありがとうございます。
guest

回答1

0

自己解決

上記ご指摘の通り、下記のように書くことで、修正出来ました。

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".view.MainActivity"> <FrameLayout android:id="@+id/frame_layout" android:layout_width="0dp" android:layout_height="match_parent" app:layout_constraintBottom_toTopOf="@id/bottom_nav_view" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/add_fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="60dp" app:fabSize="normal" tools:ignore="MissingConstraints" android:src="@drawable/plus" /> </FrameLayout> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_nav_view" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:menu="@menu/bottom_nav" /> </androidx.constraintlayout.widget.ConstraintLayout>

投稿2023/02/11 11:40

Yakusugi

総合スコア123

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問