google-developer-training / first-android-app

Repository for Build Your First Android App codelabs (Java and Kotlin)
7 stars 6 forks source link

First Android App codelab: cannot find symbol binding.buttonFirst.setOnClickListener(new View.OnClickListener() #93

Open Samaica opened 3 years ago

Samaica commented 3 years ago

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 {

private FragmentFirstBinding binding;

@Override
public View onCreateView(
        LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState
) {

    binding = FragmentFirstBinding.inflate(inflater, container, false);
    return binding.getRoot();

}

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

    binding.buttonFirst.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            NavHostFragment.findNavController(FirstFragment.this)
                    .navigate(R.id.action_FirstFragment_to_SecondFragment);
        }
    });
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    binding = null;
}

}

sudovinay01 commented 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

dargoth11 commented 2 years ago

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_ ?

sudovinay01 commented 2 years ago

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

dargoth11 commented 2 years ago

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.

sudovinay01 commented 2 years ago

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