我正在尝试使用带有底部导航视图的 androidx 导航。所以当我在 xml 文件中放置带有 android:name = "androidx.navigation.fragment.NavHostFragment" 的片段时,它给了我一个错误。我猜 xml 没有看到这个库或这个 Fragment
Here is xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".weather.WeatherActivity"
android:orientation="vertical"
android:weightSum="10"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/w_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
android:theme="@style/ToolbarColoredBackArrow"
/>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9.6"
android:id="@+id/nav_host_fragment"
app:defaultNavHost = "true"
app:navGraph = "@navigation/mobile_navigation"
android:name="androidx.navigation.fragment.NavHostFragment"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4"
app:menu="@menu/bottom_nav"
>
</com.google.android.material.bottomnavigation.BottomNavigationView>
</LinearLayout>
Activity class:
class WeatherActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(com.example.upgrade.R.layout.activity_weather)
val …Run Code Online (Sandbox Code Playgroud) android kotlin android-jetpack androidx android-jetpack-navigation