Search code examples
androidfragmentfindviewbyid

'android.view.View android.widget.TextView.findViewById(int)' on a null object reference


I get this error when I call findViewById in a fragment in android studio

" Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' on a null object reference at com.example.apptrail4.profile_frg.onCreateView() "

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    F2F_fragment = inflater.inflate(R.layout.fragment_profile_frg, container, false);

  

    String abc = "Hello sir";
    nametxt = nametxt.findViewById(R.id.nameshow);
    nametxt.setText(abc);


    return F2F_fragment;
}

Solution

  • You should use F2F_fragment.findViewById(...). You have to find child view inside parent view, that you have inflated before, not inside itself