简体   繁体   中英

Passing value from fragment to another error

i got a value from a json response and i need to send it to an other fragment to use it.

  public void onResponse(Call<String> call, Response<String> response) {

                    if (response.isSuccessful()) {
                        if (response.body() != null) {
                            Log.i("Responsestring", response.body().toString());
                            Log.i("onSuccess", response.body().toString());

                            String jsonresponse = response.body().toString();

                            try {
                                JSONObject jsonObject = new JSONObject(jsonresponse);

                                if (jsonObject.getString("status").equals("success")) {
                                    JSONObject jsonData = jsonObject.getJSONObject("data");
                                   Integer id = jsonData.getInt("id");
                                   Log.i("ID", String.valueOf(id));
                                    Toast.makeText(getActivity(), "Login Successfully!", Toast.LENGTH_SHORT)
                                            .show();
                                    Intent intent;
                                    intent = new Intent(getActivity(), ActivityListEvents.class);
                                    startActivity(intent);
                                   Log.i("DATA", jsonObject.getString("data"));
                                    Bundle bundle = new Bundle();
                                    bundle.putInt("id", id);
                                    Log.i("ID BUNDEL", String.valueOf(id));
                                    Profile_Fragment mFragment_B = new Profile_Fragment();
                                    mFragment_B.setArguments(bundle);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

Here, im getting the value that i need, i use bundle to send it to the profile fragment.

My profile fragment

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.profile_fragment, container, false);
        initViews();
        setListeners();
        Bundle bundle = getArguments();
        int id = bundle.getInt("id");
        Log.i("ID REC",String.valueOf(id));

        return view;
    }

i need the id to call it in updateUsers

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UpdateProfile.UPDPRO)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        UpdateProfile api = retrofit.create(UpdateProfile.class);
        Call<String> call = api.UpdateUsers(id,name,prenom,adresse,email,numtel);

im getting this error

'int android.os.Bundle.getInt(java.lang.String)' on a null object reference

i have searched for the problem and i didn't fix it.

as i see your code first you have to pass the data into

  Intent  intent = new Intent(getActivity(), 
   ActivityListEvents.class);
 intent.putExtras("yourKey",""+yourvalue)     
 startActivity(intent);

after this get data into your ActivityListEvents and than call this if your fragment start from ActivityListEvents

  Bundle bundle = new Bundle();
                                bundle.putInt("id", id);
                                Log.i("ID BUNDEL", String.valueOf(id));
                                Profile_Fragment mFragment_B = new Profile_Fragment();
                                mFragment_B.setArguments(bundle);

Problem solved

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if(bundle != null)
        {
            id = bundle.getInt("id");
        }
        bundle.putInt("id", id);
        Log.i("IDFROM ",String.valueOf(id));

        Bundle bundle1 = new Bundle();
        bundle1.putInt("id",id);
        final Profile_Fragment fragobj = new Profile_Fragment();
        fragobj.setArguments(bundle1);

My Fragment

Bundle bundle = this.getArguments();
        int id = bundle.getInt("id");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM