前提・実現したいこと
Android Studioでnavigation graphを利用した画面遷移/値渡しを実装したいです。
アプリはbottom navigationで3画面を遷移する内のある1画面にview pager/tab layoutで2画面に切り替えられる画面の片方の画面から、以上の画面とは異なる画面に遷移させるように実装しています。
発生している問題・エラーメッセージ
エラー表示はないですが画面遷移の際にcrashします。
該当のソースコード
遷移元のソース
kotlin
1import android.os.Bundle 2import androidx.fragment.app.Fragment 3import android.view.LayoutInflater 4import android.view.View 5import android.view.ViewGroup 6import androidx.navigation.fragment.findNavController 7import kotlinx.android.synthetic.main.fragment_tab1.view.* 8 9class Tab1Fragment : Fragment() { 10 11 override fun onCreateView( 12 inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? 13 ): View? { 14 val view = inflater.inflate(R.layout.fragment_tab1, container, false) 15 16 view.button1.setOnClickListener { 17 val num = 1 18 val action = Tab1FragmentDirections 19 .actionTab1ToDetail(num) 20 findNavController().navigate(action) 21 } 22 23 return view 24 } 25}
遷移先のソース
kotlin
1import android.os.Bundle 2import androidx.fragment.app.Fragment 3import android.view.LayoutInflater 4import android.view.View 5import android.view.ViewGroup 6import androidx.navigation.fragment.navArgs 7import kotlinx.android.synthetic.main.fragment_detail.* 8 9class DetailFragment : Fragment() { 10 11 val args: DetailFragmentArgs by navArgs() 12 13 override fun onCreateView( 14 inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? 15 ): View? { 16 val view = inflater.inflate(R.layout.fragment_detail, container, false) 17 18 textview1.text = args.toString() 19 20 return view 21 } 22}
navigation graph
xml
1<?xml version="1.0" encoding="utf-8"?> 2<navigation 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:id="@+id/navigation_graph" 6 app:startDestination="@id/navigation_home"> 7 8 <fragment 9 android:id="@+id/tab1" 10 android:name="com.example.pxc3.Tab1Fragment" 11 android:label="Tab1Fragment" 12 tools:layout="@layout/fragment_tab1"> 13 <action 14 android:id="@+id/action_tab1_to_detail" 15 app:destination="@id/detail" /> 16 </fragment> 17 18 <fragment 19 android:id="@+id/tab2" 20 android:name="com.example.pxc3.Tab2Fragment" 21 android:label="Tab2Fragment" 22 tools:layout="@layout/fragment_tab2"/> 23 24 <fragment 25 android:id="@+id/detail" 26 android:name="com.example.pxc3.DetailFragment" 27 android:label="DetailFragment" 28 tools:layout="@layout/fragment_detail"> 29 <argument 30 android:name="num" 31 app:argType="integer" /> 32 </fragment> 33</navigation>
自動生成されたTab1FragmentDirections
kotlin
1import android.os.Bundle 2import androidx.navigation.NavDirections 3import kotlin.Int 4 5class Tab1FragmentDirections private constructor() { 6 private data class ActionTab1ToDetail( 7 val num: Int 8 ) : NavDirections { 9 override fun getActionId(): Int = R.id.action_tab1_to_detail 10 11 override fun getArguments(): Bundle { 12 val result = Bundle() 13 result.putInt("num", this.num) 14 return result 15 } 16 } 17 18 companion object { 19 fun actionTab1ToDetail(num: Int): NavDirections = ActionTab1ToDetail(num) 20 } 21}
試したこと
値渡しを実装しない上でcrashしますが、下だとcrashしないので原因は画面遷移のactionであると考えています。
kotlin
1view.button1.setOnClickListener { 2// val num = 1 3// val action = Tab1FragmentDirections 4// .actionTab1ToDetail(num) 5 findNavController().navigate(R.id.action_tab1_to_detail) //上 6 findNavController().navigate(R.id.detail) //下 7}
補足情報(FW/ツールのバージョンなど)
以下のサイトを参考に実装しました。
https://qiita.com/k_masa777/items/10ab7970048236257470#%E5%80%A4%E6%B8%A1%E3%81%97%E5%87%A6%E7%90%86%E3%81%AE%E5%AE%9F%E8%A3%85
あなたの回答
tips
プレビュー