android - 片段不去活动 [Android]
问题描述
从我学习安卓到现在还不到一周。试图创建一个项目,但有些东西不起作用。
问题:不从片段移动到活动
尝试:在 Google 上浏览文档。我用@Override for onclickListener
inHomeFragment.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)
我是初学者,或者我不知道问题是什么。感谢您整天做这件事的任何帮助,但我不确定。
解决方案
您应该使用根变量来查找按钮视图 ID。
Button btn_today = (Button)root.findViewById(R.id.btn_today);
推荐阅读
- javascript - 有没有办法延迟提交表单来执行一些动画
- c# - 另一个窗口中间的 WPF 弹出窗口
- openstreetmap - OpenStreetMap osm.pbf 文件,联邦州的路径数与一个国家的路径数的比较
- javascript - 如何将一个 JS 文件链接到两个 HTML 文件?
- grafana - 在 python-influxDB-Grafana 堆栈中存储/显示每个时间戳的完整数据集
- arduino - Neopixel 的系列问题
- wordpress - 如何自定义 Woocommece 结帐页面?
- python - 使用 Firefox 配置文件和 Tor 守护程序不起作用
- redirect - 域的 DNS 级别的通配符重定向
- amazon-web-services - AWS CDK 一直要求我更新 IAM 角色,即使我已经设置了权限