[英]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.