我對整個 Android 開發和導航圖都是新手。我有一個帶有底部導航組件(片段 2-5)和 4 個導航到其他片段(片段 6-9)的按鈕的主螢屏。為了讓這個結構正常作業,我遇到了同樣的錯誤,它最終停止給出錯誤,盡管我不確定我是否正確“修復”了這個問題。這是因為我正在嘗試使用導航抽屜組件包裝活動時遇到相同的錯誤。請幫助我解決此錯誤并正確構建我的代碼。當我單擊主頁片段上的按鈕時發生錯誤(當前,導航抽屜和底部導航的行為符合預期)。
我的 main_navigation.xml(為簡潔起見省略了詳細資訊):
...
<!-- Nav graph for the 4 buttons on Home fragment-->
<include app:graph="@navigation/home_nav_graph" />
<!--start destination - also part of home_nav_graph-->
<fragment
android:id="@ id/fragment_home"
...
</fragment>
<fragment
android:id="@ id/fragment_2"
...
</fragment>
<fragment
android:id="@ id/fragment_3"
...
</fragment>
<fragment
android:id="@ id/fragment_4"
...
</fragment>
<fragment>
android:id="@ id/fragment_5"
...
</fragment>
<!--Navigation drawer-->
<fragment
android:id="@ id/fragment_10"
... />
<fragment
android:id="@ id/fragment_11"
... />
<fragment
android:id="@ id/fragment_12"
... />
</navigation>
我的 home_nav_graph.xml(負責通過按鈕從家導航到后續的 4 個片段):
<fragment
android:id="@ id/fragment_home"
... >
<action
android:id="@ id/action_home_to_fragment6"
app:destination="@id/fragment_6" />
<action
android:id="@ id/action_home_to_fragment7"
app:destination="@id/fragment_7" />
<action
android:id="@ id/action_home_to_fragment8"
app:destination="@id/fragment_8" />
<action
android:id="@ id/action_home_to_fragment9"
app:destination="@id/fragment_9" />
</fragment>
<fragment
android:id="@ id/fragment_6"
... >
<action
android:id="@ id/action_fragment6_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
<fragment
android:id="@ id/fragment_7"
... >
<action
android:id="@ id/action_fragment7_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
<fragment
android:id="@ id/fragment_8"
... >
<action
android:id="@ id/action_fragment8_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
<fragment
android:id="@ id/fragment_9"
... >
<action
android:id="@ id/action_fragment9_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
</navigation>
家庭活動 .kt 檔案:
class HomeActivity : AppCompatActivity() {
private lateinit var binding: HomeActivityBinding
private lateinit var navController: NavController
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = HomeActivityBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
// ...
// Set-up bottom navigation menu & Side menu
val navView: NavigationView = binding.navView
val bottomNavView: BottomNavigationView = findViewById(R.id.bottom_nav_view)
val drawerLayout: DrawerLayout = binding.homeLayout // from navi drawer structure
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment_home_activity) as NavHostFragment
navController = navHostFragment.navController
// val navController = findNavController(R.id.nav_host_fragment_home_activity)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.fragment_home,
R.id.fragment_2,
R.id.fragment_3,
R.id.fragment_4,
R.id.fragment_5
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
bottomNavView.setupWithNavController(navController)
}
如果您希望我附加更多 Kotlin 檔案,請告訴我哪些檔案。
uj5u.com熱心網友回復:
最終解決了我自己的問題。我將所有嵌套的目的地移動到頂層導航圖中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/447132.html
