首页 > 解决方案 > 片段不去活动 [Android]

问题描述

从我学习安卓到现在还不到一周。试图创建一个项目,但有些东西不起作用。

问题:不从片段移动到活动

尝试:在 Google 上浏览文档。我用@Override for onclickListenerinHomeFragment.java弹出一条红线唤醒remove,但还是不知道错误是什么。

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;
    }


}

我第一次创建项目时使用了导航抽屉活动

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

我是初学者,或者我不知道问题是什么。感谢您整天做这件事的任何帮助,但我不确定。

标签: androidandroid-fragmentsandroid-activityfragment

解决方案


您应该使用根变量来查找按钮视图 ID。

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

推荐阅读