Open Samaica opened 3 years ago
binding.buttonFirst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavHostFragment.findNavController(FirstFragment.this)
.navigate(R.id.action_FirstFragment_to_SecondFragment);
}
});
Just remove/comment this code.
Explanation: In 6. Task: Add views and constraints -> step:8 We have changed id of button_first to random_button The function is trying to find buttonFisrt which is not present so it throws error
Thanks sudovinay01, commenting it out worked.
I get that buttonFirst is not there anymore. But where does the button_first (with underscore and lowercase "f") id in the XML become buttonFirst (without the underscore and capital F) in the Java code? Is there some kind of declaration file or line (that I can't find) which says _button_first=buttonFirst_ ?
Thanks sudovinay01, commenting it out worked.
I get that buttonFirst is not there anymore. But where does the button_first (with underscore and lowercase "f") id in the XML become buttonFirst (without the underscore and capital F) in the Java code? Is there some kind of declaration file or line (that I can't find) which says _button_first=buttonFirst_ ?
can you specify in which step you are getting error
Thanks for replying so quickly. There is no error anymore, it compiles and runs like it is supposed to.
I was just trying to figure out how the change of the ID in the XML file break the JAVA code --- because I see no way for the two to be correlated.
Thanks for replying so quickly. There is no error anymore, it compiles and runs like it is supposed to.
I was just trying to figure out how the change of the ID in the XML file break the JAVA code --- because I see no way for the two to be correlated.
I will try telling you in simple way
The java code and XML are correlated. The XML is somewhat like front end and java is for backend.
Java naming convention is like "start with a lowercase letter and then capitalise the first letter of every subsequent word"
id given in xml "button_first" is treated as two words "button" and "first". so according to java naming convention it becomes "buttonFirst"
This is due to binding object.
Hope you get it
package com.example.myfirstapp;
import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.navigation.fragment.NavHostFragment;
import com.example.myfirstapp.databinding.FragmentFirstBinding;
public class FirstFragment extends Fragment {
}