简体   繁体   English

Android 导航抽屉项目无响应

[英]Android Navigation Drawer item without respond

The drawer opens fine and displayed correctly but nothing happens when I click on the items in the list.抽屉可以正常打开并正确显示,但是当我单击列表中的项目时没有任何反应。 Here is my code that is taken from the google tutorials (UDACITY).这是我的代码,取自谷歌教程(UDACITY)。

activity_main.xml activity_main.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
            <fragment
                android:id="@+id/myNavHostFragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:navGraph="@navigation/navigation"
                />
    </LinearLayout>
    
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/navdrawer_menu"
        app:headerLayout="@layout/nav_header"/>

    </androidx.drawerlayout.widget.DrawerLayout>

MainActivity.kt MainActivity.kt

class MainActivity : AppCompatActivity() {

private  lateinit var drawerLayout: DrawerLayout

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    @Suppress("UNUSED_VARIABLE")
    val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
    
    drawerLayout = binding.drawerLayout        
    val navController = this.findNavController(R.id.myNavHostFragment)       
    NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)        
    NavigationUI.setupWithNavController(binding.navView, navController)
}


override fun onSupportNavigateUp(): Boolean {
    val navController = this.findNavController(R.id.myNavHostFragment)        
    return NavigationUI.navigateUp(navController, drawerLayout)

navigation.xml导航.xml

I wish it helps you.我希望它对你有帮助。

<navigation 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:id="@+id/navigation"
app:startDestination="@id/titleFragment">

<fragment
    android:id="@+id/titleFragment"
    android:name="com.example.android.navigation.TitleFragment"
    android:label="fragment_title"
    tools:layout="@layout/fragment_title" >
    <action
        android:id="@+id/action_titleFragment_to_gameFragment"
        app:destination="@id/gameFragment" />
</fragment>
<fragment
    android:id="@+id/gameFragment"
    android:name="com.example.android.navigation.GameFragment"
    android:label="fragment_game"
    tools:layout="@layout/fragment_game" >
    <action
        android:id="@+id/action_gameFragment_to_gameOverFragment2"
        app:destination="@id/gameOverFragment2"
        app:popUpTo="@id/gameFragment"
        app:popUpToInclusive="true" />
    <action
        android:id="@+id/action_gameFragment_to_gameWonFragment"
        app:destination="@id/gameWonFragment"
        app:popUpTo="@id/gameFragment"
        app:popUpToInclusive="true" />
</fragment>
<fragment
    android:id="@+id/gameOverFragment2"
    android:name="com.example.android.navigation.GameOverFragment"
    android:label="fragment_game_over"
    tools:layout="@layout/fragment_game_over" >
    <action
        android:id="@+id/action_gameOverFragment_to_gameFragment"
        app:destination="@id/gameFragment"
        app:popUpTo="@id/titleFragment" />
</fragment>
<fragment
    android:id="@+id/gameWonFragment"
    android:name="com.example.android.navigation.GameWonFragment"
    android:label="fragment_game_won"
    tools:layout="@layout/fragment_game_won" >
    <action
        android:id="@+id/action_gameWonFragment_to_gameFragment"
        app:destination="@id/gameFragment"
        app:popUpTo="@id/titleFragment" />
    <argument
        android:name="numQuestions"
        app:argType="integer" />
    <argument
        android:name="numCorrect"
        app:argType="integer" />
</fragment>
<fragment
    android:id="@+id/aboutFragment"
    android:name="com.example.android.navigation.AboutFragment"
    android:label="fragment_about"
    tools:layout="@layout/fragment_about" />
<fragment
    android:id="@+id/rulesFragment"
    android:name="com.example.android.navigation.RulesFragment"
    android:label="fragment_rules"
    tools:layout="@layout/fragment_rules" />

navdrawer_menu.xml navdrawer_menu.xml

I did everything exactly like the tutorial video.我所做的一切都与教程视频完全一样。

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/rulesfragment"
    android:icon="@drawable/rules"
    android:title="@string/rules" />
<item
    android:id="@+id/aboutfragment"
    android:icon="@drawable/about_android_trivia"
    android:title="@string/about" />

You should use same id for item in navdrawer_menu.xml and destination in navigation.xml.您应该对 navdrawer_menu.xml 中的项目和 navigation.xml 中的目的地使用相同的 id。 eg when you use aboutFragment id in navigation, you should use aboutFragment instead of aboutfragment (use uppercase F) in navdrawer_menu.xml例如,当您在导航中使用 aboutFragment id 时,您应该在 navdrawer_menu.xml 中使用 aboutFragment 而不是 aboutfragment(使用大写 F)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM