2

I have two fragments.addfragment and control fragment. From addfragment I send the data using bundle with fragment transaction and then in control fragment,I want to get that data using bundle but I can't get data. so plz help me

Here is my addfragment with fragment transaction-

     relaylist.setAdapter(adapter);
relaylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


        HashMap<String, String> o = (HashMap<String, String>) relaylist.getItemAtPosition(position);



        Log.e("ip: ", "> " +  o.get("ip"));
        Log.e("port: ", "> " +  o.get("port"));
        Log.e("uname: ", "> " +  o.get("uname"));
        Log.e("password: ", "> " +  o.get("password"));


        ControlFragment fragment = new ControlFragment();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        Bundle bundle = new Bundle();
        bundle.putString("ip", o.get("ip"));
        bundle.putString("port", o.get("port"));
        bundle.putString("uname", o.get("uname"));
        bundle.putString("password", o.get("password"));
        Log.e("ip: ", "> " +  o.get("ip"));
        Log.e("port: ", "> " +  o.get("port"));
        Log.e("uname: ", "> " +  o.get("uname"));
        Log.e("password: ", "> " +  o.get("password"));
        fragment.setArguments(bundle);
        transaction.replace(R.id.mainFrame, new ControlFragment() );

        transaction.commit();
    }
});

and In control fragment I get data this way...but I can't get data

public class ControlFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView= inflater.inflate(R.layout.fragment_control, container, false);
        Bundle bundle = getArguments();

             String ip=bundle.getString("ip");
            Stirng port = bundle.getString("port");
            Stirng uname = bundle.getString("uname");
            Stirng password = bundle.getString("password");
Aditi Parikh
  • 1,522
  • 3
  • 13
  • 34
Devyani Kotadiya
  • 450
  • 5
  • 19
  • 1
    What are you getting?did you debug? – Sharath kumar Sep 08 '17 at 11:53
  • I getting error like this... java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference – Devyani Kotadiya Sep 08 '17 at 11:53
  • Possible duplicate of [How to send data from one Fragment to another Fragment?](https://stackoverflow.com/questions/24555417/how-to-send-data-from-one-fragment-to-another-fragment) – KuLdip PaTel Sep 08 '17 at 12:25

3 Answers3

4

You are setting constructor instead of object. Use below code

ControlFragment fragment = new ControlFragment();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        Bundle bundle = new Bundle();
        bundle.putString("ip", o.get("ip"));
        bundle.putString("port", o.get("port"));
        bundle.putString("uname", o.get("uname"));
        bundle.putString("password", o.get("password"));
        Log.e("ip: ", "> " +  o.get("ip"));
        Log.e("port: ", "> " +  o.get("port"));
        Log.e("uname: ", "> " +  o.get("uname"));
        Log.e("password: ", "> " +  o.get("password"));
        fragment.setArguments(bundle);
        transaction.replace(R.id.mainFrame, fragment  );

transaction.replace(R.id.mainFrame, fragment );

This is change.

Amrish Kakadiya
  • 974
  • 1
  • 7
  • 25
1

Do the fetching data before oncreate view in oncreate.Always do a null check error before setting data.Hope this helps you

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getArguments();
    if (bundle != null) {
        String ip = bundle.getString("ip");
        String port = bundle.getString("port");
        String uname = bundle.getString("uname");
        String password = bundle.getString("password");
    }
}

EDIT:

As Amrish Kakadiya pointed out

transaction.replace(R.id.mainFrame, new ControlFragment());

This line of code is creating a problem, new ControlFragment() creates an entirely new instance of the fragment and hence you are getting the getArguments() as null while retrieving.

Ajith Ramesh
  • 195
  • 14
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
0

use this line

Bundle bundle = getArguments();

before you inflater your rootview

umesh vashisth
  • 339
  • 3
  • 16