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

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

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

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

Android

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

button

HTMLで用いる<button>タグです。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

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

Q&A

解決済

1回答

1547閲覧

アンドロイドスタジオの「下部ナビゲーションアクティビティ」を使用し、Dashboardのフラグメントから 新しく作ったアクティビティへ画面遷移をしたい

Tomotaguti

総合スコア1

XML

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

Android

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

button

HTMLで用いる<button>タグです。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

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

0グッド

0クリップ

投稿2021/08/22 05:04

編集2021/08/22 21:59

前提・実現したいこと

アンドロイドスタジオの「下部ナビゲーションアクティビティ」を使用し、Dashboardのフラグメントから
新しく作ったアクティビティへ画面遷移をしたいです。下記の記入で、「空のアクティビティ」から作ったときは画面遷移ができたのですが、「下部ナビゲーションアクティビティ」では、画面遷移ができませんでした。

Kotlin初心者で、無知です。
どうかご教授お願いします。

※空のアクティビティから作ったとき同様、新規作成した時に初めから記載されたコードには触れず
コードを書き足したしました。

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

エラーメッセージ なし

該当のソースコード

Kotlin

ソースコード

activity₋main.xml <?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" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="?attr/actionBarSize"> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/nav_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" android:background="?android:attr/windowBackground" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/bottom_nav_menu" /> <fragment android:id="@+id/nav_host_fragment_activity_main" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:layout_constraintBottom_toTopOf="@id/nav_view" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/mobile_navigation" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt package com.example.myapplication import android.os.Bundle import com.google.android.material.bottomnavigation.BottomNavigationView import androidx.appcompat.app.AppCompatActivity import androidx.navigation.findNavController import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.setupActionBarWithNavController import androidx.navigation.ui.setupWithNavController import com.example.myapplication.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) val navView: BottomNavigationView = binding.navView val navController = findNavController(R.id.nav_host_fragment_activity_main) // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. val appBarConfiguration = AppBarConfiguration( setOf( R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications ) ) setupActionBarWithNavController(navController, appBarConfiguration) navView.setupWithNavController(navController) } }
fragment₋dashboard.xml <?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=".ui.dashboard.DashboardFragment"> <TextView android:id="@+id/text_dashboard" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:textAlignment="center" android:textSize="20sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="スタート" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
DashboardFragment.kt package com.example.myapplication.ui.dashboard import android.content.Intent import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider import com.example.myapplication.R import com.example.myapplication.databinding.FragmentDashboardBinding class DashboardFragment : Fragment() { private lateinit var dashboardViewModel: DashboardViewModel private var _binding: FragmentDashboardBinding? = null // This property is only valid between onCreateView and // onDestroyView. private val binding get() = _binding!! override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { dashboardViewModel = ViewModelProvider(this).get(DashboardViewModel::class.java) _binding = FragmentDashboardBinding.inflate(inflater, container, false) val root: View = binding.root val textView: TextView = binding.textDashboard dashboardViewModel.text.observe(viewLifecycleOwner, Observer { textView.text = it }) return root } override fun onDestroyView() { super.onDestroyView() _binding = null class Qualification : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.fragment_dashboard) var btnStart: Button =findViewById(R.id.btnStart) btnStart.setOnClickListener { val intent = Intent(this, SecondActivity::class.java) startActivity(intent) } } } }
activity₋second.xml <?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=".ui.dashboard.SecondActivity"> <Button android:id="@+id/backbutton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="戻る" tools:layout_editor_absoluteX="37dp" tools:layout_editor_absoluteY="26dp" /> </androidx.constraintlayout.widget.ConstraintLayout> ### 試したこと 誤字や全角スペースを防ぐため、 うまくいった空のアクティビティのコードをコピーして行ってみたが、スタートボタンからの 画面遷移ができなかった。 ### 補足情報(FW/ツールのバージョンなど)

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

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

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

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

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

tshion

2021/08/22 06:15

コードに不備がないと仮定すると、 一個気になったのは、起動しようとしているActivity は、 AndroidManifest.xml に宣言されている感じでしょうか?
hoshi-takanori

2021/08/22 06:32

DashboardFragment.kt の後ろに class Qualification : AppCompatActivity というのが追加されてますが、btnStart の処理は DashboardFragment の onCreateView に書く必要があります。 また、コードの貼り方を見直していただけると助かります。(コードブロックの前後に ``` が必要です。) https://teratail.com/help/question-tips#questionTips3-5-1
Tomotaguti

2021/08/22 08:04

返信ありがとうございます。 いただいたアドバイスをもとに宣言するため以下の文をAndroidManifest.xml に入力しました。 class Qualification : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.fragment_dashboard) var btnStart: Button =findViewById(R.id.btnStart) btnStart.setOnClickListener { val intent = Intent(this, SecondActivity::class.java) startActivity(intent) }} 入力後これらの文字が白色になっています。実行はできましたが、画面遷移はできませんでした。 恐縮ですが宣言の仕方など間違っていれば教えてください!!
Tomotaguti

2021/08/22 08:16

hoshi-takanoriさん返信ありがとうございます。 ご指摘いただいたことを参考に override fun onCreateView( のすぐ下に以下の文を記入したところ、多くのエラーを発生させてしまいました。 class Qualification : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.fragment_dashboard) var btnStart: Button =findViewById(R.id.btnStart) btnStart.setOnClickListener { val intent = Intent(this, SecondActivity::class.java) startActivity(intent) }}} 間違っていることなどご指摘していただければ嬉しいです。 PS.コードの貼り方勉強なりました。ありがとうございました。 追記などではその知識を活かせないようですので今度からの質問時活かさせていただきます!!
jimbe

2021/08/22 11:47 編集

> 追記などではその知識を活かせないようですので いえ、ご質問の編集で何度でも修正出来ます。(編集欄の下か右等にプレビューもあるはずです。リアルタイムにプレビュー出来ます。) このコメント欄には ``` は出来ませんので、コードを追加される場合はご質問を編集して追加していく形にして頂けると助かります。
odataiki

2021/08/22 14:32

質問文を編集し、コード部分の始まりと終わりを ```  で囲むとコード表示になります。 コード表示の場合 ・読みやすくなる ・コードをコピーしやすい  →検証しやすくなる つまり早期解決につながるという訳です。 他にもPythonなどインデント(空白や段揃え)が言語仕様に含まれる言語だとコード表示でないと間違いに気づきにくいなどもあります。 コード表示になっていないだけで質問文を読むことをやめてしまう方もいるようですので できるだけコード表示で記述する方が早期解決につながると思います。
Tomotaguti

2021/08/22 22:10

odataikiさんご丁寧に教えて頂きありがとうございます!
guest

回答1

0

ベストアンサー

とりあえずこんな感じでしょうか。

  • DashboardFragment の onCreateView メソッドの中に、btnStart に OnClickListener をセットする処理を書きましょう。(緑の部分)
  • Qualification というアクティビティを DashboardFragment の中に作ったようですが、おそらく不要なので消しましょう。(赤い部分)
  • (Qualification を消した場合) AndroidManifest.xml にも Qualification を書く必要はないはず。(SecondActivity をアクティビティにするなら、SecondActivity については書く必要がありますが、アクティビティの作り方によっては Android Studio が自動的に書いてくれます。)

diff

1 class DashboardFragment : Fragment() { 2 3 private lateinit var dashboardViewModel: DashboardViewModel 4 private var _binding: FragmentDashboardBinding? = null 5 6 // This property is only valid between onCreateView and 7 // onDestroyView. 8 private val binding get() = _binding!! 9 10 override fun onCreateView( 11 inflater: LayoutInflater, 12 container: ViewGroup?, 13 savedInstanceState: Bundle? 14 ): View? { 15 dashboardViewModel = 16 ViewModelProvider(this).get(DashboardViewModel::class.java) 17 18 _binding = FragmentDashboardBinding.inflate(inflater, container, false) 19 val root: View = binding.root 20 21 val textView: TextView = binding.textDashboard 22 dashboardViewModel.text.observe(viewLifecycleOwner, Observer { 23 textView.text = it 24 }) 25 26+ var btnStart: Button = binding.btnStart 27+ 28+ btnStart.setOnClickListener { 29+ val intent = Intent(requireContext(), SecondActivity::class.java) 30+ startActivity(intent) 31+ } 32 33 return root 34 } 35 36 override fun onDestroyView() { 37 super.onDestroyView() 38 _binding = null 39 } 40 } 41 42-class Qualification : AppCompatActivity() { 43- override fun onCreate(savedInstanceState: Bundle?) { 44- super.onCreate(savedInstanceState) 45- setContentView(R.layout.fragment_dashboard) 46- 47- var btnStart: Button =findViewById(R.id.btnStart) 48- 49- btnStart.setOnClickListener { 50- val intent = Intent(this, SecondActivity::class.java) 51- startActivity(intent) 52- } 53- } 54-}

なお、Bottom Navigation テンプレートですが、

  • Home, Dashboard, Notification の各画面はフラグメントなので、フラグメントの扱いについて学びましょう。
  • Home, Dashboard, Notification の各画面に対して ViewModel があるので、それについても学ぶ必要が…。
  • 画面遷移は Navigation を使ってるので、それについても…。
  • 各画面の view は View Binding を使ってます。(findViewById してもいいけど…。)
  • activity₋main.xml のレイアウトが最初からおかしい (画面上部に無駄なスペースがあるなど) ので、自分で修正する必要があります。

という感じで、初心者にはちょっと荷が重い気がします。

投稿2021/08/22 17:31

hoshi-takanori

総合スコア7895

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

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

Tomotaguti

2021/08/22 22:08

ご丁寧な解説をしていただきありがとうございます。また、ご指摘の通りよく理解していない部分が多いのでまずはフラグメントやviewModel、Navigationなどを学ぼうと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問