无法使用导航故事板在片段中添加按钮以从一个片段跳转到另一个片段
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);
我正在尝试在片段页面中添加一个按钮,以使用 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);