1

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;
}
Ravi
  • 21
  • 2
  • Where did you define `nametxt `? – Long Luong Aug 26 '20 at 19:38
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – TofferJ Aug 26 '20 at 20:22
  • I define "nametxt" in public class profile extrnd, when i used nametxt = F2F_fragment.findViewById()cthen it works.... – Ravi Aug 26 '20 at 20:28

1 Answers1

1

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

Warrocker
  • 734
  • 6
  • 12
  • 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference i get this error when i use setOnclickListner – Ravi Aug 26 '20 at 20:25