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

1

4 Answers 4

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,

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.