Why Android Studio shows setupWithNavController not exists?

戏子无情 提交于 2020-07-08 12:47:12

问题


NavigationUI.setupWithNavController(bottomNavigationView, navController)

When I run the app, the code is fine, but why it keeps showing compile error like this ?

The bottom navigation I use is in com.google.android.material:material, and the bottom navigation in the param is android.support.design.widget.BottomNavigationView. I know they are the same thing, but why it complains ?


回答1:


Just restart Android Studio. The lint gets confused sometimes.




回答2:


Had the same issue. It's super confusing with all the different android packages now. androidx, android-arch, android-support. All of them in some weird version state with -rcX, alpha-X or beta-X... Using the following code I got the same error. But after telling AndroidStudio to File -> Invalidate Caches and Restart, it suddenly worked.

Also I can highly recommend this tutorial: https://proandroiddev.com/android-jetpack-navigationui-a7c9f17c510e

imports:

implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha07"
implementation "android.arch.navigation:navigation-ui-ktx:1.0.0-alpha07"

MainActivity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.NavigationUI
import com.google.android.material.bottomnavigation.BottomNavigationView
import net.onefivefour.android.ebtimetracker.R

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        NavigationUI.setupWithNavController(findViewById(R.id.navigation), findNavController(R.id.navHostFragment))
    }

    override fun onSupportNavigateUp() = findNavController(R.id.navHostFragment).navigateUp()
}

Activity Layout

<fragment
    android:id="@+id/navHostFragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@+id/navigation"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/nav_graph" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>

nav_graph.xml

<activity
    android:id="@+id/mainActivity"
    android:name="net.onefivefour.android.ebtimetracker.ui.MainActivity"
    android:label="MainActivity"
    tools:layout="@layout/activity_main" />

<fragment
    android:id="@+id/quickEntryFragment"
    android:name="net.onefivefour.android.ebtimetracker.ui.quickentry.QuickEntryFragment"
    android:label="QuickEntryFragment"
    tools:layout="@layout/fragment_quick_entry" />
<fragment
    android:id="@+id/templatesFragment"
    android:name="net.onefivefour.android.ebtimetracker.ui.templates.TemplatesFragment"
    android:label="TemplatesFragment"
    tools:layout="@layout/fragment_templates" />
<fragment
    android:id="@+id/entriesFragment"
    android:name="net.onefivefour.android.ebtimetracker.ui.entries.EntriesFragment"
    android:label="EntriesFragment"
    tools:layout="@layout/fragment_entries" />

</navigation>



回答3:


Please make sure that you correctly applied the three installation steps correctly

1- Add the dependencies to your app build.gradle

dependencies {
    def nav_version = "2.1.0-alpha02"

    implementation "androidx.navigation:navigation-fragment:$nav_version" // For Kotlin use navigation-fragment-ktx
    implementation "androidx.navigation:navigation-ui:$nav_version" // For Kotlin use navigation-ui-ktx
}

2- For Safe args, add the following classpath in your top level build.gradle file

buildscript {
    repositories {
        google()
    }
    dependencies {
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0-alpha02"
    }
}

3- You must also apply one of two available plugins.

apply plugin: "androidx.navigation.safeargs" // or for Kotlin only: apply plugin: "androidx.navigation.safeargs.kotlin" 

I had forgotten to apply the navigation safe args in my app build.gradle and it caused me to get this error.

I hope this was helpful to you. :)

Source: https://developer.android.com/jetpack/androidx/releases/navigation#declaring_dependencies



来源:https://stackoverflow.com/questions/52768334/why-android-studio-shows-setupwithnavcontroller-not-exists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!