gpt4 book ai didi

java - 将值从 fragment 传递到另一个错误

转载 作者:行者123 更新时间:2023-12-01 17:53:33 24 4
gpt4 key购买 nike

我从 json 响应中获取了一个值,我需要将其发送到其他 fragment 才能使用它。

  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 将其发送到配置文件 fragment 。

我的个人资料 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;
}

我需要 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)' on a null object reference

我已经搜索过这个问题,但没有解决。

最佳答案

当我首先看到你的代码时,你必须将数据传递到

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

在此之后,将数据获取到您的 ActivityListEvents 中,如果您的 fragment 从 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);

关于java - 将值从 fragment 传递到另一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60757809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com