简体   繁体   中英

Parsing data from FragmentActivity to already created Fragment

I have a FragmentActivity and three Fragments. I am using a Viewpager in my FragmentActivity to show all of my fragments one by one as user swipes the screen. Further I am able to get some string data into my FragmentActivity using intent.getExtras().getString() from a separate Adapter. Now I want to send this string data from FragmentActivity to my first Fragment using a user defined function.But I am getting a NullPointerException. Someone please take a look at my code and suggest me some solution.

public class ExpandedPopupActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_expanded_popup);


    Intent i = this.getIntent();
    String Nam = i.getExtras().getString("name");
    String Sur = i.getExtras().getString("surname");
    String Emp = i.getExtras().getString("empID");
    String Slr = i.getExtras().getString("salary");

   if(Nam!=null && Sur!=null && Emp!=null && Slr!=null ){
       FragmentOne f = new FragmentOne();
       f.getDetail(Nam,Sur,Emp,Slr);


    ViewPager pager = (ViewPager)findViewById(R.id.popuppager);
    FragmentAdapter fadapter= new FragmentAdapter(getSupportFragmentManager());
    pager.setAdapter(fadapter);

}

This is my Fragment Class

      public class FragmentOne extends Fragment {
        TextView name,surname,emp,sal;

        public void getDetail(string name,String surname,String empid,String salary){
             name.setText(name);
             surname.setText(surname);
             emp.setText(empid);
             sal.setText(salary);
    }
        @Override
        public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_one,container,false);

            name = (TextView)view.findViewById(R.id.name);
            surname = (TextView)view.findViewById(R.id.s_name);
            emp = (TextView)view.findViewById(R.id.emp_id);
            sal = (ImageView)view.findViewById(R.id.salary);
            return view;
    }

 }

This is what log says

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

You can perform the same operation in the fragment that you just did in the activity.

For example , in order to fetch the name in your fragment , do this.

name = getActivity().getIntent().getExtras().getString("name");

You can do this , in either onCreateView or onCreate method of the fragment.

The problem here is that you want to set the value of the TextViews but they are not initialized.

Something like below:

public class FragmentOne extends Fragment {

String name, surname, emp, sal,
TextView tvName, tvSurname, tvEmp, tvSal;

public void getDetail(string name,String surname,String empid,String salary){
 this.name = name;
 this.surname= surname;
 this.emp= empid;
 this.sal= salary;
} 

@Override public View onCreateView(LayoutInflater inflater, ViewGroup 
     container, Bundle savedInstanceState) {
     View view = 
     inflater.inflate(R.layout.fragment_one,container,false);

     tvName= (TextView)view.findViewById(R.id.name);
     tvSurname= (TextView)view.findViewById(R.id.s_name);
     tvEmp= (TextView)view.findViewById(R.id.emp_id);
     tvSal= (ImageView)view.findViewById(R.id.salary);

     name.setText(name);
     tvSurname.setText(surname);
     tvEmp.setText(empid);
     tvSal.setText(salary);

     return view;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM