简体   繁体   English

将数据从FragmentActivity解析到已创建的Fragment

[英]Parsing data from FragmentActivity to already created Fragment

I have a FragmentActivity and three Fragments. 我有一个FragmentActivity和三个Fragment。 I am using a Viewpager in my FragmentActivity to show all of my fragments one by one as user swipes the screen. 我在FragmentActivity中使用Viewpager,以便在用户轻扫屏幕时一一显示所有片段。 Further I am able to get some string data into my FragmentActivity using intent.getExtras().getString() from a separate Adapter. 此外,我还可以使用来自单独适配器的intent.getExtras()。getString()将一些字符串数据输入到FragmentActivity中。 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. 现在我想使用用户定义的函数将此字符串数据从FragmentActivity发送到我的第一个Fragment。但是我收到了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. 您可以在片段的onCreateView或onCreate方法中执行此操作。

The problem here is that you want to set the value of the TextViews but they are not initialized. 这里的问题是您想设置TextViews的值,但是它们没有初始化。

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;
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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