0

I have implemented the fragment in android. Now we try to send the data from activity to the fragment. But while sending the data we are getting the NullPointerException in *onCreateView* method of fragment.

Following code of Activity.

 setContentView(R.layout.fragment_1);
        FragmentManager fm = getFragmentManager();
        FragmentTranstction ft = fm.beginTransaction();
        Fragment1 f1 = new Fragment1();
                    String text1 = "prakash";
        Bundle bundle = new Bundle();
        bundle.putString("sessionName", text1);
        f1.setArguments(bundle);
        ft.add(R.id.frag1, f1);
        ft.commit();

Following code of Fragment:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = this.getArguments().getString("sessionName");    
    TextView sessionTitle = (TextView) getView()
            .findViewById(R.id.session1);
    sessionTitle.setText(strtext);
    return inflater.inflate(R.layout.fragment_1, container, false);
}

what is the mistake i did, because of that i am getting NullPointerException, please help

PTech
  • 163
  • 1
  • 11
  • change the string getting code to onActivityCreate() override method. its come after the oncreateview – Nithinlal Jun 23 '14 at 06:04
  • for communication between Activity and Fragment watch this : http://stackoverflow.com/questions/24321449/android-navigation-drawer-unable-to-settext-in-a-fragment-from-mainactivity/24322359#24322359 – Umang Kothari Jun 23 '14 at 06:06

3 Answers3

0

Try below code:-

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_1, container, false);
    String strtext = this.getArguments().getString("sessionName");    
    TextView sessionTitle = (TextView) view.findViewById(R.id.session1);
    sessionTitle.setText(strtext);
    return view;
}

your not getting view and try to set value on textview.

duggu
  • 37,851
  • 12
  • 116
  • 113
0

You have

 TextView sessionTitle = (TextView) getView()
        .findViewById(R.id.session1);

getView() returns null in your case. So sessionTitle is null and when you call setText() on null you get NUllPointerException.

SO change to

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = (VIew)inflater.inflate(R.layout.fragment_1, container, false); 
    // inflate tha layout
    String strtext = this.getArguments().getString("sessionName");   
    // get the string  
    TextView sessionTitle = (TextView) view
            .findViewById(R.id.session1);
     // initialize textview or initialize it in onActivityCreated using getView
    sessionTitle.setText(strtext);
    // set text to textview
    return view
}

You can use getView().findViewById(id) in onActivityCreated()

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

while getting the id of textview you are getting nullpointer exception,because of using getview(),getview is used when we get the id in onActivityCreated method(). just change getview() to view will remove this exception.just chnage :

TextView sessionTitle = (TextView)view.findViewById(R.id.session1);
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59