无法使用 NavigationUI 创建底部导航

时间:2021-07-08 08:27:57

标签: java android

我只是按照互联网上的底部导航教程进行操作,但遇到了 NavigationUI.setupActionBarWithNavController 并且 NavigationUI.setupWithNavController 不起作用。 enter image description here

我的项目链接:https://github.com/kiluss2/Food-App-Ex

非常感谢您的帮助 T.T

//gradle 项目

    plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    aaptOptions.cruncherEnabled = false
    aaptOptions.useNewCruncher = false

    defaultConfig {
        applicationId "com.kiluss.foodappex"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    implementation 'com.github.amarjain07:StickyScrollView:1.0.2'

    implementation 'androidx.navigation:navigation-fragment:2.3.5'
    implementation 'androidx.navigation:navigation-ui:2.3.5'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

}

//MainActivity.java

package com.kiluss.foodappex;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;


public class MainActivity extends AppCompatActivity {

private ActionBar toolbar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

//Initialize Bottom Navigation View.
BottomNavigationView navView = findViewById(R.id.bottomNav_view);

//Pass the ID's of Different destinations
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
        R.id.homeFragment, R.id.favoriteFragment, R.id.accountFragment)
        .build();
//Initialize NavController.
NavController navController = Navigation.findNavController(this, R.id.navHostFragment);
NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration); // Cannot resolve symbol 'setupActionBarWithNavController'
NavigationUI.setupWithNavController(navView,navController); //Cannot resolve symbol 'setupActionBarWithNavController'
}

//nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<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/mobile_navigation"
app:startDestination="@id/homeFragment">


<fragment
    android:id="@+id/homeFragment"
    android:name="com.kiluss.foodappex.HomeFragment"
    android:label="fragment_home"
    tools:layout="@layout/fragment_home" />
<fragment
    android:id="@+id/favoriteFragment"
    android:name="com.kiluss.foodappex.FavoriteFragment"
    android:label="fragment_favorite"
    tools:layout="@layout/fragment_favorite" />
<fragment
    android:id="@+id/accountFragment"
    android:name="com.kiluss.foodappex.HomeFragment"
    android:label="fragment_account"
    tools:layout="@layout/fragment_home" />
</navigation>

//菜单文件

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_account"
        android:icon="@drawable/ic_account"
        android:title="@string/title_favorite" />

    <item
        android:id="@+id/navigation_favourites"
        android:icon="@drawable/ic_heart"
        android:title="@string/title_account" />
</menu>

1 个答案:

答案 0 :(得分:1)

您收到 Cannot resolve symbol 错误,因为您在 MainActivity 中的所有代码都在 void onCreate(Bundle savedInstanceState) 方法之外。

只需将您的代码更改为如下所示:

public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Initialize Bottom Navigation View.
        BottomNavigationView navView = findViewById(R.id.bottomNav_view);
        //Initialize NavController.
        NavController navController = Navigation.findNavController(this, R.id.navHostFragment);
        NavigationUI.setupWithNavController(navView,navController);
    }
}