为了账号安全,请及时绑定邮箱和手机立即绑定

无法使用导航故事板在片段中添加按钮以从一个片段跳转到另一个片段

无法使用导航故事板在片段中添加按钮以从一个片段跳转到另一个片段

阿晨1998 2021-06-18 18:51:01
我正在尝试使用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 回答

?
温温酱

TA贡献1752条经验 获得超4个赞

改变线下

Button goToBtn = (Button) this.findViewById(R.id.goToBtn);

Button goToBtn = (Button) view.findViewById(R.id.goToBtn);


查看完整回答
反对 回复 2021-06-30
  • 1 回答
  • 0 关注
  • 116 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信