0

I'm new to android, trying to send a bundle from a Activity to Fragment using

Bundle args = new Bundle();
args.putString("name", "XXXXXXX");

FragmentTab1 fTab1 = new FragmentTab1();
fTab1.setArguments(args);

and in FragmentTab1 onCreate method as follows:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    // Get the view from fragmenttab1.xml
    View view = inflater.inflate(R.layout.fragmenttab1, container, false);
    Bundle bundle = this.getArguments();
    if (bundle != null) {
      name = bundle.getString("name");
    }
    TextView nameView = (TextView) view.findViewById(R.id.dinesh);
    nameView.setText(name);
    return view;
  }

getting null pointer exception at Bundle bundle = this.getArguments();

please help me to trace this exception

Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
user1374429
  • 1
  • 1
  • 3
  • Have a look at this discussion (: http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment – Trinimon Aug 23 '13 at 09:42

4 Answers4

1

sorry if my answer is late for you..

Please create first instance of fragment then bundle..and make sure perform transaction on fragment to order to get argument in fragment.

FragmentTab1 fTab1 = new FragmentTab1();
Bundle args = new Bundle();
args.putString("name", "XXXXXXX");
fTab1.setArguments(args);

Hope this will resolve null pointer exceptions, you can get argument on fragment's onStart or OnCreateView method..

Kind Regards,

kj007
  • 6,073
  • 4
  • 29
  • 47
1

Try this

Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

and in Fragment Task

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("edttext");    
    return inflater.inflate(R.layout.fragment, container, false);
}
Akshay Mukadam
  • 2,388
  • 1
  • 27
  • 40
0

Create a Static method in your fragment and just pass the Bundle argument to that method that is the simplest method I have found till now.

below Would be a format of the method in your fragment

Bundle arg;
public static void setArguments(Bundle Args)
{


    arg=Args;

}

and from where you want to set the Arguments just call in this fashion

YourFragment.setArguments(Args);

Hope You will find this answer helpful

Akshay Borgave
  • 112
  • 2
  • 15
0

Same thing happened with me while working with android studio. However, it is a fault of android studio that it sometimes doesn't add updated code to apk. So it would be better if you run build->Clean and then run the application.