Android导航抽屉项目无响应

时间:2020-08-16 21:55:06

标签: android android-studio navigation drawerlayout

抽屉打开正常并正确显示,但是当我单击列表中的项目时没有任何反应。这是我的代码,取自Google教程(UDACITY)。

activity_main.xml

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)

MainActivity.kt

<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" />

navigation.xml

希望它能对您有所帮助。

<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" />

navdrawer_menu.xml

我所做的一切都与教程视频完全一样。

{{1}}

1 个答案:

答案 0 :(得分:0)

您应该对navdrawer_menu.xml中的项目和destination.navigation.xml中的项目使用相同的ID。 例如在导航中使用aboutFragment id时,应在navdrawer_menu.xml中使用aboutFragment而不是aboutfragment(使用大写F)