繁体   English   中英

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

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

我试图在片段页面中添加一个按钮,以使用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为红色。

在行下更改

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM