Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot find FirstFragmentDirections & SecondFragmentArgs

From: https://codelabs.developers.google.com/codelabs/build-your-first-android-app/#8

AndroidStudio 3.6.2

Last tasks in the step-by-step guide are producing the following errors:

cannot find a symbol variable SecondFragmentArgs package FirstFragmentDirections does not exist cannot find symbol variable FirstFragmentDirections

Here is a sample of the code from SecondFragment.java:

public class SecondFragment extends Fragment {
    Integer myArg = SecondFragmentArgs.fromBundle(getArguments()).getMyArg();
    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

Here is the code from FirstFragment.java:

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    view.findViewById(R.id.random_button);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextView showCountTextView = view.getRootView().findViewById(R.id.textview_first);
            int currentCount = Integer.parseInt(showCountTextView.getText().toString());
            FirstFragmentDirections.action_FirstFragment_to_SecondFragment action = FirstFragmentDirections.actionFirstFragmentToSecondFragment(currentCount);
            NavHostFragment.findNavController(FirstFragment.this).navigate(R.id.action_FirstFragment_to_SecondFragment);
        }
    });

    view.findViewById(R.id.toast_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast myToast = Toast.makeText(getActivity(), "Hello toast!", Toast.LENGTH_SHORT);
            myToast.show();
        }
    });

    view.findViewById(R.id.count_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            countMe(view);
        }
    });

}
}
like image 491
Z80 Avatar asked Oct 22 '25 16:10

Z80


1 Answers

I faced the same issue, it turned out the codelab was referencing an alpha version of SafeArgs in the Build.gradle file. To fix it, just include the latest classpath in your top level Build.gradle file.

buildscript {
    repositories {
        google()
    }
    dependencies {
        def nav_version = "2.3.0"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}

Also, do not forget to apply plugin to your app or module's Build.gradle file:

apply plugin: "androidx.navigation.safeargs" if using Java or

apply plugin: "androidx.navigation.safeargs.kotlin" for Kotlin

EDIT: For the sake of completeness, you also need to Rebuild Gradle for the FirstFragmentDirections generated class to be added. That can be done by choosing Build > Make Project in the tool menu.

Consult with the official docs below, as they offer additional troubleshooting if the above is not working. Referenced Docs

https://developer.android.com/guide/navigation/navigation-pass-data

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

like image 86
Amani Arman Avatar answered Oct 25 '25 07:10

Amani Arman