0

I am trying to pass JSONArray from one fragment to another but i am getting NullPointerException.

Fragment 1

JSONArray bankarray = jsonobject1.getJSONArray("banks");
Fragment f = new SendMoneyRecipientFragment();
Bundle b = new Bundle();
b.putString("banklist", banks);
f.setArguments(b);

Fragment 2

getArguments().getString("banklist");

Really appreciate your help.

  • Where do you have NullPointerException? What is the "banks" in b.putString("banklist", banks);? – Sergio Apr 15 '17 at 07:51
  • banks is JSONArray String. – Rajesh Vishnani Apr 15 '17 at 08:09
  • I can't see where you have NullPointerException without the full code of your fragments. At what line NullPointerException is appeared? – Sergio Apr 15 '17 at 08:17
  • look at this stackoverflow post: [How to send data from one Fragment to another Fragment?](http://stackoverflow.com/questions/24555417/how-to-send-data-from-one-fragment-to-another-fragment) – Edalat Feizi Apr 15 '17 at 08:32
  • look at this stackoverflow post: [How to send data from one Fragment to another Fragment?](http://stackoverflow.com/questions/24555417/how-to-send-data-from-one-fragment-to-another-fragment) – Edalat Feizi Apr 15 '17 at 08:33

1 Answers1

0

You can use Activity for that. For example: Fragment1

public class Fragment1 extends Fragment {

    private String mJSON;
    private Context mContext;

    public static Fragment1 instance() {
        return new Fragment1();
    }

    @Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        mContext = (IContext) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString() + " must implement " + IContext.class.getName());
    }
}

    //... some code to fetch JSON and set it to mJSON


    private void onSomeButtonClick() {
        if (mContext != null) {
            mContext.showFragment2(mJSON);
        }
    }
}

Fragment2

public class Fragment2 extends Fragment {
    private static final String KEY_JSON = "key_json";

    public static Fragment2 instance(String json) {
        Bundle args = new Bundle();
        args.putString(KEY_JSON, json);
        Fragment2 f = new Fragment2();
        f.setArguments(args);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_layout, container, false);
    }

    @Override
    public void onViewCreated(View root, Bundle savedInstanceState) {
        super.onViewCreated(root, savedInstanceState);
        String json = getArguments().getString(KEY_JSON);

        //do whatever you want with json
    }
}

IContext

public interface IContext {
    void showFragment2(String json);
}

Your Activity

public class YourActivity extends AppCompatActivity implements IContext {
    //... some code to initialize - onCreate() ...


    @Override
    public void showFragment2(String json) {
        Fragment fr2 = Fragment2.instance(json);
        FragmentTransaction tr = getSupportFragmentManager().beginTransaction()
            .replace(R.id.fragment_container, fr2, fr2.getClass().getName());
        //add to backstack if need tr.addToBackStack(null);
        tr.commitAllowingStateLoss();
    }
}
Sergio
  • 27,326
  • 8
  • 128
  • 149