gpt4 book ai didi

java - 发送到 Android 中的另一个 fragment 后, bundle 对象显示 null

转载 作者:行者123 更新时间:2023-12-01 12:36:36 29 4
gpt4 key购买 nike

我正在开发android应用程序。我将Fragment类扩展到我的应用程序中的所有java类。我想将数据从 fragment 发送到另一个 fragment (AllDeals-> CarDetails)。为了发送数据,我创建parcelable类对象并发送该对象到另一个 fragment 。但在新 fragment 中它总是显示空数据。我尝试了很多时间,但没有成功。我已经在这方面浪费了很多时间,所以我决定发布一个问题。所以我请求你解决我的问题。我像这样初始化 CarBean

 obj = new CarBean();
obj.setName("deepak");
obj.setPrize(txt_carprize.getText().toString());
obj.setAvgPrize(txt_vat.getText().toString());
obj.setImage("R.drawable.img"+currentposition);

这是我的代码

 public class AllDeals extends BaseFragment implements OnClickListener 
{
android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BaseFragment fragment = new AllDeals();
bundle.putParcelable("cardetails", obj);
fragment.setArguments(bundle);

fragmentTransaction.commit();
}

这个类我读了Bundled object

public class CarDetails extends BaseFragment implements OnClickListener
{
Bundle bundledata=getArguments();
CarBean carbean=bundledata.getParcelable("cardetails");
String name=carbean.getName();
}

这是 Java Bean 类

public class CarBean implements Parcelable
{
String name,prize,avgprize,image;



public CarBean(Parcel in)
{
name=in.readString();
prize=in.readString();
avgprize=in.readString();
image=in.readString();
}
public CarBean()
{
// TODO Auto-generated constructor stub
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}

public void setPrize(String prize)
{
this.prize=prize;
}
public String getPrize()
{
return prize;
}
public void setAvgPrize(String avgprize)
{
this.avgprize=avgprize;
}

public String getAvgPrize()
{
return avgprize;
}

public void setImage(String image)
{
this.image=image;

}

public String getImage()
{
return image;
}

@Override
public int describeContents()
{
// TODO Auto-generated method stub
return 0;
}

/*
here we store the data into parcle object

*/
@Override
public void writeToParcel(Parcel out, int flags)
{
// TODO Auto-generated method stub
out.writeString(name);
out.writeString(prize);
out.writeString(avgprize);
out.writeString(image);
}


/* retrive the data from parcel object */






public static final Parcelable.Creator<CarBean> CREATOR = new Parcelable.Creator<CarBean>()
{

@Override
public CarBean createFromParcel(Parcel in)
{
// TODO Auto-generated method stub
/* this method call CarBean Constructor */

return new CarBean(in);
}

@Override
public CarBean[] newArray(int size)
{
// TODO Auto-generated method stub
return new CarBean[size];
}
};
}

最佳答案

在此之前打印/记录您的obj

bundle.putParcelable("cardetails", obj);

确保它不为空。

关于java - 发送到 Android 中的另一个 fragment 后, bundle 对象显示 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25526107/

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