cannot find FirstFragmentDirections & SecondFragmentArgs

Viewed 2898

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);
        }
    });

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

  1. Project Structure -> Gradle Version needs to be at least 6.1.1.
  2. Open your Gradle sidebar on the far right (or double shift -> Gradle).
  3. Click the "Toggle Offline Mode" so it's off.
  4. This could start a massive Gradle download, wait until finished.
  5. Go to Build -> Make
  6. Don't forget to sync.
Related