Problem upgrading my Fragment's Navigation version(from 2.3.5 to 2.4.0-alpha03)
Asked Answered
I

2

0

I have been trying to upgrade my fragment's navigation version from 2.3.5 to 2.4.0-alpha03 so that it can support multiple back stacks as per the documentation https://developer.android.com/jetpack/androidx/releases/navigation

and also help in saving and restoring the state of each of my bottom nav items for a few days now unsuccessfully. Each time I sync the implementations in my build.gradle, code in my homeactivity throws this error cannot resolve symbol

final NavController navController = navHostFragment.getNavController(); 
NavigationUI.setupWithNavController(bottomNavigationView, navController);
FirstFragment firstFragment = (FirstFragment) navHostFragment.getChildFragmentManager().getFragments().get(0);

and the imports for them

import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

throw an error too unused import statement, cannot resolve symbol. So if I run the app, it's unable to perform the above functions that I want it to.

Here's what i've tried so far:

  • I've added this implementation "androidx.navigation:navigation-fragment:2.4.0-alpha03" and implementation "androidx.navigation:navigation-ui:2.4.0-alpha03" to my build.gradle(app).

Yet it didn't fix.

EDIT

My Android Gradle Plugin version is 4.0.2

My Gradle Version is 6.1.1

I believe there's something I didn't do right or something I'm meant to do that I didn't do. Please I'd appreciate it if anyone can identify that.

Here's my specific codes:

Build.gradle(app):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.viz.lightweatherforecast"
        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 {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility 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'
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation "androidx.navigation:navigation-fragment:2.4.0-alpha03"
    implementation "androidx.navigation:navigation-ui:2.4.0-alpha03"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.media2:media2:1.0.0-alpha04'
    implementation 'org.jetbrains:annotations:15.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

Build.gradle(Project):

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.2"

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

HomeActivity.java:

package com.viz.lightweatherforecast.Activity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.viz.lightweatherforecast.FirstFragment;
import com.viz.lightweatherforecast.R;
import com.viz.lightweatherforecast.Retrofit.ApiClient;
import com.viz.lightweatherforecast.Retrofit.ApiInterface;
import com.viz.lightweatherforecast.Retrofit.Example;

import org.jetbrains.annotations.NotNull;

import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeActivity extends AppCompatActivity {
    // Last update time, click sound, search button, search panel.
    TextView time_field;
    MediaPlayer player;
    ImageView Search;
    EditText textfield;
    // For scheduling background image change(using constraint layout, start counting from dubai, down to statue of liberty.
    ConstraintLayout constraintLayout;
    public static int count=0;
    int[] drawable =new int[]{R.drawable.dubai,R.drawable.central_bank_of_nigeria,R.drawable.eiffel_tower,R.drawable.hong_kong,R.drawable.statue_of_liberty};
    Timer _t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        // use home activity layout.

        time_field = findViewById(R.id.textView9);
        Search = findViewById(R.id.imageView4);
        textfield = findViewById(R.id.textfield);
        //  find the id's of specific variables.

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
        // host 3 fragments along with bottom navigation.
        final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
        assert navHostFragment != null;
        final NavController navController = navHostFragment.getNavController();
        NavigationUI.setupWithNavController(bottomNavigationView, navController);


        // For scheduling background image change
        constraintLayout = findViewById(R.id.layout);
        constraintLayout.setBackgroundResource(R.drawable.dubai);
        _t = new Timer();
        _t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                // run on ui thread
                runOnUiThread(() -> {
                    if (count < drawable.length) {

                        constraintLayout.setBackgroundResource(drawable[count]);
                        count = (count + 1) % drawable.length;
                    }
                });
            }
        }, 5000, 5000);

        Search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // make click sound when search button is clicked.
                player = MediaPlayer.create(HomeActivity.this, R.raw.click);
                player.start();

                getWeatherData(textfield.getText().toString().trim());
                // make use of some fragment's data
                FirstFragment firstFragment = (FirstFragment) navHostFragment.getChildFragmentManager().getFragments().get(0);
                firstFragment.getWeatherData(textfield.getText().toString().trim());

            }

            private void getWeatherData(String name) {

                ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);

                Call<Example> call = apiInterface.getWeatherData(name);

                call.enqueue(new Callback<Example>() {
                    @Override
                    public void onResponse(@NonNull Call<Example> call, @NonNull Response<Example> response) {

                        try {
                            assert response.body() != null;
                            time_field.setVisibility(View.VISIBLE);
                            time_field.setText("Last Updated:" + " " + response.body().getDt());
                        } catch (Exception e) {
                            time_field.setVisibility(View.GONE);
                            time_field.setText("Last Updated: Unknown");
                            Log.e("TAG", "No City found");
                            Toast.makeText(HomeActivity.this, "No City found", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(@NotNull Call<Example> call, @NotNull Throwable t) {
                        t.printStackTrace();
                    }

                });
            }

        });
    }
}

Activity_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout"
    android:background="@drawable/dubai"
    tools:context=".Activity.HomeActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_menu" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="409dp"
        android:layout_height="599dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:navGraph="@navigation/my_nav"
        />

    <EditText
        android:id="@+id/textfield"
        android:layout_width="250dp"
        android:layout_height="35dp"
        android:autofillHints="@string/change_city"
        android:background="@color/colorPrimary"
        android:hint="@string/search_city"
        android:inputType="text"
        android:labelFor="@id/imageView4"
        android:padding="8dp"
        android:textColor="@color/colorAccent"
        android:textSize="16sp"
        app:layout_constraintEnd_toStartOf="@+id/imageView4"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="50dp"
        android:layout_height="35dp"
        android:layout_marginEnd="1dp"
        android:contentDescription="@string/searchbtn"
        android:src="@drawable/look"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/time_field"
        android:visibility="gone"
        android:textColor="#FFFFFF"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textfield" />


</androidx.constraintlayout.widget.ConstraintLayout>
Ity answered 19/6, 2021 at 0:11 Comment(0)
S
1

I am also working on the same thing first of all don't use 2.4.0 alpha-03 because they have a problem .

Copy-paste from the developer page

Known Issue

Safe Args will fail with an Unable to find method ‘’java.lang.String kotlin.text.CarsKt.titleCase(char, java.util.Locale)’’ error when using Gradle 6.7.0 due to a dependency on an older version of Kotlin. This can be worked around by updating to use Gradle 7.0. (b/190739257)

So this will fix in 2.4.0 alpha04 till then use 2.4.0 alpha02

So this is not the actual problem try to downgrade and update your question with the latest logs so I can help you that

Stcyr answered 25/6, 2021 at 14:11 Comment(5)
Okay thanks, I will work on this and give you feedbackIty
While trying to update, I'm getting this error unable to find method 'org.gradle.api.artifacts.result.componentselectionreason.getDescriptionIty
Have you added this plugin :- androidx.navigation:navigation-safe-args-gradle-pluginStcyr
No. I haven't, let me tryIty
Downgrading gradle back to 6.1.1 and downgrading both my navigation-fragment and UI to 2.4.0-alpha01 made the fix. Thanks a lot man and to everyone who contributed in their own way to help me fix this issue!Ity
C
1

The problem is that you're mixing 2.3.5 Navigation dependencies with 2.4.0-alpha03 dependencies - they all need to be exactly the same version.

You aren't using Compose, so you don't need a dependency on navigation-compose - you should remove that dependency (and also the Safe Args one if you are not using Safe Args either).

Instead, you should just upgrade your existing Navigation dependencies:

    implementation "androidx.navigation:navigation-fragment:2.4.0-alpha03"
    implementation "androidx.navigation:navigation-ui:2.4.0-alpha03"
Cana answered 19/6, 2021 at 0:19 Comment(6)
When I tried exactly this, the error even got worse. Now even import androidx.navigation.fragment.NavHostFragment; and import androidx.navigation.ui.NavigationUI; is showing cannot resolve symbolIty
Please update your question with the exact build.gradle you're using.Cana
You didn't update any of the versions like this answers said to do. Please update those and include your updated build.gradle in your question.Cana
Ok I will. But please it's not easy for me to be updating code every moment. Like I said I'm in college and currently taking exams so I have very limited time for coding everyday. I already initially included all that I tried earlier on. Will I keep including all Suggestions? That's quite stressful but let me do this one anyway.Ity
Assuming the build.gradle you've posted is exactly what you're using, you forgot a - in your navigation-fragment dependency. It must be exactly androidx.navigation:navigation-fragment:2.4.0-alpha03.Cana
Let us continue this discussion in chat.Ity
S
1

I am also working on the same thing first of all don't use 2.4.0 alpha-03 because they have a problem .

Copy-paste from the developer page

Known Issue

Safe Args will fail with an Unable to find method ‘’java.lang.String kotlin.text.CarsKt.titleCase(char, java.util.Locale)’’ error when using Gradle 6.7.0 due to a dependency on an older version of Kotlin. This can be worked around by updating to use Gradle 7.0. (b/190739257)

So this will fix in 2.4.0 alpha04 till then use 2.4.0 alpha02

So this is not the actual problem try to downgrade and update your question with the latest logs so I can help you that

Stcyr answered 25/6, 2021 at 14:11 Comment(5)
Okay thanks, I will work on this and give you feedbackIty
While trying to update, I'm getting this error unable to find method 'org.gradle.api.artifacts.result.componentselectionreason.getDescriptionIty
Have you added this plugin :- androidx.navigation:navigation-safe-args-gradle-pluginStcyr
No. I haven't, let me tryIty
Downgrading gradle back to 6.1.1 and downgrading both my navigation-fragment and UI to 2.4.0-alpha01 made the fix. Thanks a lot man and to everyone who contributed in their own way to help me fix this issue!Ity

© 2022 - 2024 — McMap. All rights reserved.