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. However, in the Mainfragment.java, my button id cannot be resolved, it cannot recognize my button id and I don't know why. 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.
change below line
Button goToBtn = (Button) this.findViewById(R.id.goToBtn);
With
Button goToBtn = (Button) view.findViewById(R.id.goToBtn);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.