我正在尝试使用android studio的导航故事板的方法在片段页面中添加一个按钮以从一个片段跳转到另一个片段。但是,在 Mainfragment.java 中,我的按钮 id 无法解析,它无法识别我的按钮 id,我不知道为什么。请帮忙! public MainFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_main, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Button goToBtn = (Button) this.findViewById(R.id.goToBtn); goToBtn.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.toAccount, null)); } }对于 中的上述代码Mainfragment.java,findViewById是红色的。
1 回答
![?](http://img1.sycdn.imooc.com/54586431000103bb02200220-100-100.jpg)
温温酱
TA贡献1752条经验 获得超4个赞
改变线下
Button goToBtn = (Button) this.findViewById(R.id.goToBtn);
和
Button goToBtn = (Button) view.findViewById(R.id.goToBtn);
添加回答
举报
0/150
提交
取消