je2
je2

Reputation: 45

fragment does not go to activity [Android]

It's been less than a week since I learned Android. trying to create a project, but there are some things that don't work.

Problem: Does not move from fragment to activity

Tried: Browse the document on Google. and I used @Override for onclickListener in HomeFragment.java A red line popped up and woke up remove, but still don't know what the error is.

HomeFragment.java

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;
    private Button btn_today;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);

        ViewGroup root_view = (ViewGroup)inflater.inflate(R.layout.fragment_home, container, false);

        Button btn_today = (Button)root_view.findViewById(R.id.btn_today);

        btn_today.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
            Intent intent = new Intent(getActivity(), HomeActivity.class);
            startActivity(intent);
            }
        });

        return root;
    }


}

used the Navigation Drawer Activity when I first created the project.

what I want to do is click on the button btn_today to go to the activity page (in xml file)

I'm a beginner or I don't know what the problem is. appreciate any help doing it all day, but I'm not sure.

Upvotes: 0

Views: 58

Answers (1)

Jayesh Kanani
Jayesh Kanani

Reputation: 76

You should be use root variable for finding button view id.

Button btn_today = (Button)root.findViewById(R.id.btn_today);

Upvotes: 1

Related Questions