I have two similar by onViewCreated
structure fragments
//1st
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
textDebug1 = getActivity().findViewById(R.id.view_debug1); //from layout1
textDebug2 = getActivity().findViewById(R.id.view_debug2); //from layout2
//2nd
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
textDebug1 = getActivity().findViewById(R.id.view_debug1); //<--gives error; layout1
textDebug2 = getActivity().findViewById(R.id.view_debug2); //from layout2
in onCreateView for both I have same line but different layouts.
View rootView = inflater.inflate(R.layout.activity_frag_main, container, false);
TextView
s are located on different layouts and if I choose to load 1st fragment then code works well, but if I choose 2nd to run 1st then getActivity()
returns null
. What might cause this if creation is identical for both?