-2

I am using Bundle to passing data from my First TabFragment but it prompts out with the NullPointerException. Error is occur when getArguments() in list_fragments2 in second tab

MainActivity Fragment

list_fragment2 fragment = new list_fragment2();
Bundle b = new Bundle();
b.putString("test","text");
fragment.setArguments(b);
Toast.makeText(this, "" + b, Toast.LENGTH_SHORT).show();

SecondActivity Fragment

 public class list_fragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View h = inflater.inflate(R.layout.tab2, container, false);
        TextView textView = (TextView) h.findViewById(R.id.textView);

        Bundle bundle=getArguments();

        //your string
        if(bundle != null) {
            String test = bundle.getString("test");
            textView.setText(test);
        }
            return h;

    }
}
Gopal
  • 1,734
  • 1
  • 14
  • 34
AnthonyTang
  • 89
  • 1
  • 1
  • 9

2 Answers2

1

Do you actually load your second fragment?

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

list_fragment2 fragment = new list_fragment2();
Bundle b = new Bundle();
b.putString("test","text");
fragment.setArguments(b);
fragmentTransaction.replace(placeholder, fragment);
fragmentTransaction.commit();
beeb
  • 1,615
  • 1
  • 12
  • 24
  • thanks , i try it before but i still get nullpointerexception this error – AnthonyTang Jan 03 '17 at 03:47
  • public class list_fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View h = inflater.inflate(R.layout.tab2, container, false); TextView textView = (TextView) h.findViewById(R.id.textView); Bundle bundle=getArguments(); //your string if(bundle != null) { String test = bundle.getString("test"); textView.setText(test); } return h; } } – AnthonyTang Jan 03 '17 at 03:51
  • Please edit your originial post with an "Update" section – beeb Jan 03 '17 at 03:53
  • Can't find an issue in `list_fragment2`. Can you post your current code of the `Mainactivity` too? Please update your original post again. – beeb Jan 03 '17 at 04:04
  • can i have i more question? is it possible to do it from Mainactivity tablayout using bundle pass data to tab fragment? – AnthonyTang Jan 03 '17 at 04:25
  • I don't even know your class structur. Normally you have a viewpager that provides swiping between the tabs. An adapter handles the loading of the tabs. And there you can pass a bundle too. You also can program manager classes/static variables or callbacks to pass/save information. If you have another question please open a new thread because you accepted this answer as the solution. – beeb Jan 03 '17 at 11:55
0

I think you can create your instance of list_fragment2 code within list_fragment2. Maybe your codes recreated list_fragment2 many times.

public static list_fragment2 createInstance() {
        list_fragment2 fragment = new list_fragment2();
        Bundle bundle = new Bundle();
        bundle .putString("test","text");
        fragment.setArguments(bundle);
        return fragment;
    }
RoShan Shan
  • 2,924
  • 1
  • 18
  • 38