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

我正在尝试使用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是红色的。


阿晨1998
浏览 117回答 1
1回答

温温酱

改变线下Button goToBtn = (Button) this.findViewById(R.id.goToBtn);和Button goToBtn = (Button) view.findViewById(R.id.goToBtn);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java