Skip to content Skip to sidebar Skip to footer

Error: Cannot Find Symbol Import Androidx.navigation.fragment.navargs

I was doing the 'Build Your First Android App in Java' from the website https://codelabs.developers.google.com/codelabs/build-your-first-android-app/#8. Everything worked until I g

Solution 1:

This is an error with the codelab, pulled in from the Kotlin version of the code lab which uses the by navArgs() Kotlin property delegate to retrieve the arguments (as explained in the Using Safe Args guide).

This isn't needed in Java, as you use the SecondFragmentArgs.fromBundle(getArguments()) method to retrieve the arguments class.

Therefore, you can just skip that import: it isn't used anywhere and can safely be ignored.

Solution 2:

if you are following building your first app in java and you encountered this error,than try commenting or deleting following import

import androidx.navigation.fragment.navArgs;

Solution 3:

You have two build.gradle in your build scripts section. One of them is your project wide file and the other is your module one.

In your project one, you have to have this line inside your depencies:

    dependencies {
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0-alpha04"
    }

in your other file, the build.gradle that is for your module, you have to add this following line (it is usually on top of it with the other lines alike):

apply plugin: 'androidx.navigation.safeargs.kotlin'

On other note, as you are starting to learn, I strongly recommend that you learn Kotlin instead of Java. Kotlin is the newly Google supported language to develop Android Apps.

Let me know if it fixes your issue.

Solution 4:

This is an error with the Java tutorial because of the newer Android Studio versions, on the Kotlin one, it doesn't give you that problems.

try to go to the "SecondFragment.java" file and add this in the line: @SuppressLint("StringFormatInvalid" //line that evades the formatting problem.

before the onViewCreated method, so it would end like this:

@SuppressLint("StringFormatInvalid")publicvoidonViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Integercount= SecondFragmentArgs.fromBundle(getArguments()).getMyArg();
    StringcountText= getString(R.string.random_heading, count);
    TextViewheaderView= view.getRootView().findViewById(R.id.textview_header);
    headerView.setText(countText);
    Randomrandom=newjava.util.Random();
    IntegerrandomNumber=0;

    if (count > 0) {
        randomNumber = random.nextInt(count + 1);
    }

    TextViewrandomView= view.getRootView().findViewById(R.id.textview_random);
    randomView.setText(randomNumber.toString());

    view.findViewById(R.id.button_second).setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View view) {
            NavHostFragment.findNavController(SecondFragment.this)
                    .navigate(R.id.action_SecondFragment_to_FirstFragment);
        }
    });

All the other code after that line I told you is the same as the tutorial (I put it just in case tho), hope it helps ;).

Post a Comment for "Error: Cannot Find Symbol Import Androidx.navigation.fragment.navargs"