Google's documentation highlighting how to build an android app in java (https://developer.android.com/codelabs/build-your-first-android-app#8) returns an error when using intents. On step 7, it says:
FirstFragmentDirections.ActionFirstFragmentToSecondFragment action = FirstFragmentDirections.actionFirstFragmentToSecondFragment(currentCount);
but my IDE consistently returns an error saying: Cannot resolve symbol 'ActionFirstFragmentToSecondFragment'. Ive tried resyncing the gradle files but im continuing to get this error. I've followed the steps to a T from my understanding but still unable to find the cause of the issue.
CodePudding user response:
I figured out the issue. It turns out in the nav_graph.xml file, I added the argument to the wrong fragment. I added the argument to fragment 1 because I assumed we should add it to the sending fragment. However, it needs to be added to the receiving fragment (fragment 2). In order to fix this:
- Navigate to your nav_graph.xml file
- Delete Current attributes
- Add attributes to the receiving fragment. eg: if you are sending a value from fragment 1 to fragment 2, fragment 2 should have the argument added.
For understanding: When adding an attribute, classes are generated to allow communicating between fragments.
If I add an argument to fragment 2, the following classes will be generated:
- FirstFragmentDirections
- SecondFragmentArgs
- SecondFragmentDirections
If I add an argument to fragment 1, the following classes will be generated:
- SecondFragmentDirections
- FirstFragmentArgs
- FirstFragmentDirections
With each class containing the necessary methods to navigate to other classes. This is the reason I got the error previously: "Cannot resolve symbol 'ActionFirstFragmentToSecondFragment" I was trying to navigate from the first fragment to the second fragment, but with the classes that had been generated, the method that I attempted to use was not available.