Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Android App codelab: TASK 9 #80

Open
Lifshatz opened this issue Dec 1, 2020 · 9 comments
Open

First Android App codelab: TASK 9 #80

Lifshatz opened this issue Dec 1, 2020 · 9 comments

Comments

@Lifshatz
Copy link

Lifshatz commented Dec 1, 2020

It's possible I made a mistake or perhaps there's an error with the instructions. Either way, I am looking for some help with this: https://developer.android.com/codelabs/build-your-first-android-app?hl=tr#8
Step 7: Send the count to the second fragment
The Next/Random button was set up by Android Studio to go from the first fragment to the second, but it doesn't send any information. In this step you'll change it to send a number for the current count. You will get the current count from the text view that displays it, and pass that to the second fragment.

Open FirstFragment.java (app > java > com.example.myfirstapp > FirstFragment)
Find the method onViewCreated() and notice the code that sets up the click listener to go from the first fragment to the second.
Replace the code in that click listener with a line to find the count text view, textview_first.

int currentCount = Integer.parseInt(showCountTextView.getText().toString());
Create an action with currentCount as the argument to actionFirstFragmentToSecondFragment().

FirstFragmentDirections.ActionFirstFragmentToSecondFragment action = FirstFragmentDirections.actionFirstFragmentToSecondFragment(currentCount);
Add a line to find the nav controller and navigate with the action you created.

NavHostFragment.findNavController(FirstFragment.this).navigate(action);
Here is the whole method, including the code you added earlier:

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

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

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);
}
});
}
Screen Shot 2020-12-01 at 2 08 17 AM (2)
Screen Shot 2020-12-01 at 2 13 43 AM (2)
Screen Shot 2020-12-01 at 2 09 06 AM (2)

@Lifshatz Lifshatz changed the title First Android App codelab: First Android App codelab: TASK 9 > Step 7 Dec 1, 2020
@Lifshatz Lifshatz changed the title First Android App codelab: TASK 9 > Step 7 First Android App codelab: TASK 9 Dec 1, 2020
@Lifshatz
Copy link
Author

Lifshatz commented Dec 1, 2020

Okay, so I have a weird habit of being able to fix things/find a solution soon after I reach out for help and it appears that is the case here (haha).
Just in case someone else needs help with this in the future, here's the steps for resolution:
1. Go back to "build.gradle (Module: MyFirst_App.app)" and ensure the following appears immediately under the "plugins" brackets:
apply plugin: 'com.android.application'
apply plugin: 'androidx.navigation.safeargs'
image

*Note: The codelab instructions state to input the following under the already existing "apply plugin" lines, however mine did not have ANY "apply plugin" already existing in this file, so I had to guess. I originally put this under the dependency bracket (which is incorrect). * Screen shot for context below:
image

2. Now, Sync your Gradle files (screen shot below)
image

3. Once it's synced, click Build > Make Project
image

*Notice "SecondFragmentArgs" class will now appear under the java (generated) > com.example.myfirstapp folder/section
image

4. Go back to FirstFragment.java and verify the "ActionFirstFragmentToSecondFragment" line is no longer underlined in red (neither is "currentCount" or "action" per the line below).

image

@jawadzd
Copy link

jawadzd commented Dec 3, 2020

hey there, I tried your steps but didn't end up with any solution for my problem as I didn't change anything
I still have the "ActionFirstFragmentToSecondFragment" underlined and in red generating an error

@bass212drum
Copy link

Follow Step 6: Create the argument for the navigation action
Step 6 "In the navigation graph" = nav_graph.xml, do the operation like "Add Argument dialog" in nav_graph.xml , not in SecondFragment.kt
If you Add Argument dialog right, you won't have "Too many arguments" problem.

The text is NOT clear, I made the same mistake like you did. I try to find "Add Argument dialog" first in "SecondFragment.kt" since the text said "Click on SecondFragment" but I can't find "Add Argument dialog" , then I look at " fragment_second.xml" then " FirstFragment.kt" then "fragment_first.xml" then I asked a person who knows kotlin and he didn't know why there was this "Too many arguments" problem.

This problem took me a week to solve by myself and people can call me stupid since the text saide "In the navigation graph" but for a total beginner, it is better off the professional who wrote this task can write it more clearly. "In the nav_graph.xml" is more clear than "In the navigation graph".

Hopefull this will help you.

@jawadzd
Copy link

jawadzd commented Dec 6, 2020

Thanks for the reply but things just got messed up for me now, I am writing the code using java not using Kotlin so if I didn't understand you right check my repo for the app and tell me specifically in what file and where to fix my error

@vagdonic
Copy link

Okay, so I have a weird habit of being able to fix things/find a solution soon after I reach out for help and it appears that is the case here (haha).
Just in case someone else needs help with this in the future, here's the steps for resolution:
1. Go back to "build.gradle (Module: MyFirst_App.app)" and ensure the following appears immediately under the "plugins" brackets:
apply plugin: 'com.android.application'
apply plugin: 'androidx.navigation.safeargs'
image

*Note: The codelab instructions state to input the following under the already existing "apply plugin" lines, however mine did not have ANY "apply plugin" already existing in this file, so I had to guess. I originally put this under the dependency bracket (which is incorrect). * Screen shot for context below:
image

2. Now, Sync your Gradle files (screen shot below)
image

3. Once it's synced, click Build > Make Project
image

*Notice "SecondFragmentArgs" class will now appear under the java (generated) > com.example.myfirstapp folder/section
image

4. Go back to FirstFragment.java and verify the "ActionFirstFragmentToSecondFragment" line is no longer underlined in red (neither is "currentCount" or "action" per the line below).

image

This was really helpful, thanks!!!!

@mikalaikarol
Copy link

Didn't help me though *(

@mikalaikarol
Copy link

if worked for me when I placed
apply plugin: 'com.android.application'
apply plugin: "androidx.navigation.safeargs"
to the very top of the graddle (Module):app file, and removed any plugins {...} info there.

Having both plugins {...} and apply plugin caused an error
Screenshot 2021-03-10 at 11 44 54

Also, make sure you add argument in nav_graph to Second fragment, not to First fragment

@Jimmyrchen
Copy link

apply plugin: 'com.android.application'
apply plugin: "androidx.navigation.safeargs"

OMG, Your Last sentence helps me a lot. The error showed only because I add argument in nav_graph to the First Fragement which should have been added to the Second Fragement. Thank you @mikalaikarol

@skgoetz
Copy link

skgoetz commented Oct 19, 2021

Thank you, @Lifshatz! For me the app-level build.gradle already had a plugins section at its top with com.android.application.
Thus, instead of adding apply plugin: , I've added an id line for safeargs:

plugins {
    id 'com.android.application'
    id 'androidx.navigation.safeargs'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants