-3

i have started to learn android recently and i was looking at a fragment example in a video tutorial in the tutorial they haven't made the textview static but when i tried to implement fragment with some changes in the code i started to get null pointer exception my code looks like this

public class BlankFragment2 extends Fragment {

TextView tv;

public BlankFragment2() {
    // 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_blank_fragment2, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    tv= (TextView) getActivity().findViewById(R.id.txt2);
}



public void setName(String name){
    //tv=(TextView)getActivity().findViewById(R.id.txt2);
    if(tv == null){
        System.out.println("tv is null");
    }
    tv.setText("Hi "+name);
}
}

i am getting null pointer exception at settext unless i make the textview static can someone please explain why

this is not duplicate of what is NPE i know what NPE is and why it shows up my question here is that after initializing the textview in onActivityCreated why it is null again in setname function

pawn doe
  • 53
  • 5
  • 3
    NO . In which layout `R.id.txt2` exists ? – ADM Apr 18 '18 at 08:50
  • in fragment_blank_fragment2 – pawn doe Apr 18 '18 at 09:00
  • @ADM this is definitely not a duplicate of what is NPE i know why NPE comes, i have updated the question to explain the problem in better way – pawn doe Apr 18 '18 at 09:52
  • This does not make any difference Ultimately you are accessing a Null object which will gives you NPE. The root cause is `getActivity()` points to parent `Activity` and your view is part of fragment not activity. – ADM Apr 18 '18 at 10:02
  • @ADM i also tried View view = getView(); then view.findViewById(R.id.txt2) still getting NPE – pawn doe Apr 18 '18 at 10:04
  • Just use the solution right below. Read [Creating a Fragment](https://developer.android.com/guide/components/fragments.html#Creating). – ADM Apr 18 '18 at 10:06

3 Answers3

1

NullPointerException is thrown when an application attempts to use an object reference that has the null value.

FYI

  • You should return VIEW's object
  • Check findViewById(R.id.txt2 . Make sure you this ID present in XML.

Don't

tv= (TextView) getActivity().findViewById(R.id.txt2);

Do

 tv= (TextView) view.findViewById(R.id.txt2);

Try this way

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
             View view= inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
             tv= (TextView) view.findViewById(R.id.txt2);
            return view;
        }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

Not getActivity().findViewById(), it must be view.findViewById() because Activity.findViewById() will look for a View in the Activity layout, whereas view.findViewById() will look for the View in the Fragment layout.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View inflatedView = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
    return inflatedView;
}

And in onViewCreated you can initialise your TextView

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // here goes your intialisation using view
    tv= (TextView) view.findViewById(R.id.txt2);
}

Make sure you have a TextView with the id txt2 in fragment_blank_fragment2.xml

2Dee
  • 8,609
  • 7
  • 42
  • 53
Vivek_Neel
  • 1,343
  • 1
  • 14
  • 25
0

Sorry guys it was a stupid mistake on my side, in my mainactivity i wrote

BlankFragment2 frag2 = new BlankFragment2();
    frag2.setName(name);

i corrected it by writing

BlankFragment2 frag2=(BlankFragment2)getFragmentManager().findFragmentById(R.id.frag2);
    fragb.setName(name);

now it is working

pawn doe
  • 53
  • 5