简体   繁体   English

无法使用导航情节提要板在片段中添加按钮以从一个片段跳转到另一个片段

[英]Unable to add a button in fragment using Navigation storyboard to jump from one fragment to another fragment

I am trying to add a button in a fragment page to jump from one fragment to another fragment using the method of the navigation storyboard of android studio. 我试图在片段页面中添加一个按钮,以使用android studio导航情节提要的方法从一个片段跳转到另一个片段。 However, in the Mainfragment.java, my button id cannot be resolved, it cannot recognize my button id and I don't know why. 但是,在Mainfragment.java中,我的按钮ID无法解析,无法识别我的按钮ID,也不知道为什么。 Please help! 请帮忙!

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


    }
    }

For the above codes in Mainfragment.java , the findViewById is red. 对于Mainfragment.java的上述代码, findViewById为红色。

change below line 在行下更改

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

With

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过单击一个片段中的按钮并更改android studio中的底部导航图标,从一个片段重定向到另一个片段 - redirection from one fragment to another by clicking a button in one fragment together with changing the bottom navigation icon in android studio Androidx:使用一个片段中的按钮导航到另一个片段 - Androidx: Using a button in one fragment to navigate to another fragment 片段通过滑动以及单击按钮从一个片段移动到另一个片段 - Fragment move from one Fragment to another by sliding as well as button click 如何在点击返回按钮时从一个片段中调用另一个片段? - How to call another fragment from one fragment on hitting back button? Android-将日期从一个片段添加到另一个正在运行的片段 - Android - add date from one fragment to another running fragment 如何使用 android 中的按钮从另一个片段打开片段 - How to open a fragment from another fragment using a button in android 如何从一个活动跳到另一个活动并设置片段 - How to jump from one activity to another and set a fragment 单击新片段后是否单击另一个片段? - New fragment on button click from another fragment? 用另一个片段中的按钮打开片段 - Open fragment with button from another fragment 如何从一个片段移动到另一个片段? - How to move from one fragment to another fragment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM