簡體   English   中英

將值從片段傳遞給另一個錯誤

[英]Passing value from fragment to another error

我從 json 響應中獲得了一個值,我需要將它發送到另一個片段才能使用它。

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

在這里,我得到了我需要的值,我使用 bundle 將它發送到配置文件片段。

我的個人資料片段

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

我需要 id 在 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);

我收到這個錯誤

'int android.os.Bundle.getInt(java.lang.String)' 在一個空對象引用上

我已經搜索了問題,但沒有解決。

當我首先看到您的代碼時,您必須將數據傳遞給

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

在此之后將數據放入您的 ActivityListEvents 中,如果您的片段從 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);

問題解決了

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

我的片段

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM