Closed YesayaSoftware closed 5 years ago
Do you have this import statement?
import androidx.navigation.ui.setupWithNavController
I did @linucksrox, but it show like it is unused.
Can you post the entire MainActivity.kt file? Also did you add the proper kotlin extensions in the project's build.gradle file, and add the correct dependencies in the app build.gradle file? I just got everything working in my environment today by using the dependencies used here: https://github.com/ResoCoder/forecast-mvvm-android-kotlin/commit/f4434b47c2530e2e0fffaac9e0f1ecd36c6ff402
The exact versions are important too, it's not always safe to assume you should use newer dependency versions just because they are available.
`package software.yesaya.weather.ui
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.navigation.NavController import androidx.navigation.Navigation import androidx.navigation.ui.NavigationUI import androidx.navigation.ui.setupWithNavController import kotlinx.android.synthetic.main.activity_main.* import software.yesaya.weather.R
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
navController = Navigation.findNavController(this, R.id.nav_host_fragment)
bottom_nav.setupWithNavController(navController)
NavigationUI.setupActionBarWithNavController(this, navController)
}
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(null, navController)
}
} `
I'm not sure.... I would double check in activity_main.xml that you have this at the bottom of your LinearLayout:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav" />
And that you created bottom_nav.xml for the menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/currentWeatherFragment"
android:icon="@drawable/ic_today"
android:title="Today"/>
<item
android:id="@+id/futureListWeatherFragment"
android:icon="@drawable/ic_calendar_week"
android:title="7 Days"/>
<item
android:id="@+id/settingsFragment"
android:icon="@drawable/ic_settings"
android:title="Settings"/>
</menu>
And that you created a navigation resource called mobile_navigation:
<?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/currentWeatherFragment">
<fragment
android:id="@+id/currentWeatherFragment"
android:name="software.yesaya.weather.ui.weather.current.CurrentWeatherFragment"
android:label="current_weather_fragment"
tools:layout="@layout/current_weather_fragment" />
<fragment
android:id="@+id/futureListWeatherFragment"
android:name="software.yesaya.weather.ui.weather.future.list.FutureListWeatherFragment"
android:label="future_list_weather_fragment"
tools:layout="@layout/future_list_weather_fragment" />
<fragment
android:id="@+id/futureDetailWeatherFragment"
android:name="software.yesaya.weather.ui.weather.future.detail.FutureDetailWeatherFragment"
android:label="future_detail_weather_fragment"
tools:layout="@layout/future_detail_weather_fragment" />
<fragment
android:id="@+id/settingsFragment"
android:name="software.yesaya.weather.ui.settings.SettingsFragment"
android:label="SettingsFragment" />
</navigation>
If you have all of that, double check project level build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.10'
ext.room_version = '2.1.0-alpha02'
ext.navigation_version = '1.0.0-alpha07'
ext.kodein_version = '5.2.0'
ext.lifecycle_version = '2.0.0'
ext.retrofit_version = '2.4.0'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0-alpha01'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha07'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
And app build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"
apply plugin: 'androidx.navigation.safeargs'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.dalydays.android.forecastmvvm"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
// Navigation
implementation "android.arch.navigation:navigation-fragment:$navigation_version"
implementation "android.arch.navigation:navigation-ui:$navigation_version"
implementation "android.arch.navigation:navigation-fragment-ktx:$navigation_version"
implementation "android.arch.navigation:navigation-ui-ktx:$navigation_version"
implementation "androidx.core:core-ktx:1.0.1"
implementation "androidx.constraintlayout:constraintlayout:1.1.3"
// Room
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.legacy:legacy-support-v4:1.0.0"
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
kapt "androidx.room:room-compiler:$room_version"
// Gson
implementation "com.google.code.gson:gson:2.8.5"
// Kotlin Android Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0-RC1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0-RC1"
// Retrofit
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
// ViewModel
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// Kodein
implementation "org.kodein.di:kodein-di-generic-jvm:$kodein_version"
implementation "org.kodein.di:kodein-di-framework-android-x:$kodein_version"
// Better dateTime-time support even on older Android versions
implementation "com.jakewharton.threetenabp:threetenabp:1.1.0"
// Glide
implementation 'com.github.bumptech.glide:glide:4.8.0'
kapt 'com.github.bumptech.glide:compiler:4.8.0'
// Groupie RecyclerView
implementation 'com.xwray:groupie:2.1.0'
implementation 'com.xwray:groupie-kotlin-android-extensions:2.1.0'
// Preference
implementation "androidx.preference:preference:1.0.0"
// Location
implementation "com.google.android.gms:play-services-location:16.0.0"
// New Material Design
implementation "com.google.android.material:material:1.0.0"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}
If all your code looks good, try clicking on the menu Build -> Rebuild Project. If that doesn't work, you can also try File -> Invalidate Caches/Restart... And post what version of Android Studio you're using
Thanks @linucksrox , I double check each file and Rebuild the project then Invalidate Caches/Restart. Now am good to go. Thanks
Hi, I m getting the same issue.