我正在尝试使用Parcelable将onDataLoaded函数(在MainActivity中)的自定义对象列表传递给片段。但是我得到了这个错误...
这是一个MainActivity funkcion,onDataLoaded
@Override
public void onDataLoaded(List<Grad> gradovi, List<Ponuda> ponude) {
Spinner spinnerGradovi = (Spinner) findViewById(R.id.gradovi_spinner);
ArrayAdapter<String> adapterGradovi;
List<String> listaGradova = new ArrayList<>();
ArrayList<Ponuda> ponudaArrayList = new ArrayList<Ponuda>();
ponudaLista = ponude;
gradLista = gradovi;
for(Grad grad : gradovi ){
listaGradova.add(grad.getNaziv());
}
for(Ponuda ponuda : ponude){
ponudaArrayList.add(ponuda);
}
//here I want to get arguments which contains ArrayList
Fragment fragmentGet = svePonudeFragment;
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Ponuda", ponudaArrayList);
fragmentGet.setArguments(bundle);
adapterGradovi = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listaGradova);
spinnerGradovi.setAdapter(adapterGradovi);
}
这是碎片:
public class SvePonudeFragment extends Fragment {
private RecyclerView rv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sve_ponude_fragment, container, false);
rv = (RecyclerView) rootView.findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
Bundle data = getActivity().getIntent().getExtras();
ArrayList<Ponuda> listaPonuda= data.getParcelable("ponude");
for(Ponuda ponuda : listaPonuda){
System.out.println(ponuda.getNaziv());
}
initializeAdapter(listaPonuda);
return rootView;
}
private void initializeAdapter(List<Ponuda> preuzetePonude){
RVAdapter adapter = new RVAdapter(preuzetePonude);
rv.setAdapter(adapter);
}
}
这是对象类
@Table(database = MainDatabase.class)
public class Ponuda extends BaseModel implements Parcelable{
@Column
@PrimaryKey int id;
@Column String tekstPonude;
@Column int cijena;
@Column int popust;
@Column int cijenaOriginal;
@Column String urlSlike;
@Column int usteda;
@Column String kategorija;
@Column String grad;
@Column String datumPonude;
public Ponuda() {
}
public Ponuda(int id,String tekstPonude, int cijena, int popust, int cijenaOriginal, String urlSlike, int usteda, String kategorija, String grad, String datumPonude){
this.id=id;
this.tekstPonude = tekstPonude;
this.cijena = cijena;
this.popust = popust;
this.cijenaOriginal = cijenaOriginal;
this.urlSlike = urlSlike;
this.usteda = usteda;
this.kategorija = kategorija;
this.grad = grad;
this.datumPonude = datumPonude;
}
public String getNaziv() {
return tekstPonude;
}
public String getCijena() {
return Integer.toString(cijena);
}
public String getURL() {
return urlSlike;
}
public void setCijena(int cijena) {
this.cijena = cijena;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTekstPonude() {
return tekstPonude;
}
public void setTekstPonude(String tekstPonude) {
this.tekstPonude = tekstPonude;
}
public int getPopust() {
return popust;
}
public void setPopust(int popust) {
this.popust = popust;
}
public int getCijenaOriginal() {
return cijenaOriginal;
}
public void setCijenaOriginal(int cijenaOriginal) {
this.cijenaOriginal = cijenaOriginal;
}
public String getUrlSlike() {
return urlSlike;
}
public void setUrlSlike(String urlSlike) {
this.urlSlike = urlSlike;
}
public int getUsteda() {
return usteda;
}
public void setUsteda(int usteda) {
this.usteda = usteda;
}
public String getKategorija() {
return kategorija;
}
public void setKategorija(String kategorija) {
this.kategorija = kategorija;
}
public String getGrad() {
return grad;
}
public void setGrad(String grad) {
this.grad = grad;
}
public String getDatumPonude() {
return datumPonude;
}
public void setDatumPonude(String datumPonude) {
this.datumPonude = datumPonude;
}
public static List<Ponuda> getAll(){
List<Ponuda> ponudaList;
ponudaList= new Select().from(Ponuda.class).queryList();
return ponudaList;
}
public Ponuda(Parcel in){
String[] data = new String[9];
in.readStringArray(data);
this.id = in.readInt();
this.tekstPonude = in.readString();
this.cijena = in.readInt();
this.popust = in.readInt();
this.cijenaOriginal = in.readInt();
this.urlSlike = in.readString();
this.usteda = in.readInt();
this.kategorija = in.readString();
this.grad = in.readString();
this.datumPonude = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(tekstPonude);
dest.writeInt(cijena);
dest.writeInt(popust);
dest.writeInt(cijenaOriginal);
dest.writeString(urlSlike);
dest.writeInt(usteda);
dest.writeString(kategorija);
dest.writeString(grad);
dest.writeString(datumPonude);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Ponuda createFromParcel(Parcel in) {
return new Ponuda(in);
}
public Ponuda[] newArray(int size) {
return new Ponuda[size];
}
};
}
我不明白为什么我会得到空指针异常
尝试在空对象引用上调用虚拟方法'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)'
答案 0 :(得分:0)
是的,你收到了这个错误。为什么?因为你有2个错误:
Bundle data = getActivity().getIntent().getExtras();
- &GT;你试图获得Activity
的捆绑,而不是你的碎片。您应该尝试使用getArguments()
。
您将收到另一个错误,因为当调用片段的onCreateView()
时,片段中没有参数包(您的包在您的活动的onDataLoaded中设置)。根据我的意见,您应该在片段中重新加载数据,如:
public void reloadData(){
//getArguments, parse and show in the list
}
//then in your onDataLoaded of MainActivity:
//here I want to get arguments which contains ArrayList
((YourFragment)fragmentGet).reloadData(); //call after set argument
答案 1 :(得分:0)
你可以用另一种方式实现这个目标。 从活动中获取数据。
在主java中添加updateListInFragment()。 MainActivity.java
public ArrayList<Ponuda> updateListInFragment(){
return ponudaArrayList;
}
SvePonudeFragment.java
private ArrayList<Ponuda> mMyList;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sve_ponude_fragment, container, false);
rv = (RecyclerView) rootView.findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
Bundle data = getActivity().getIntent().getExtras();
updateArrayList(); //Let's update here
for(Ponuda ponuda : mMyList){
System.out.println(ponuda.getNaziv());
}
initializeAdapter(listaPonuda);
return rootView;
}
// Update your mMyList from activity
public void updateArrayList(){
MainActivity activity = (MainActivity) activity;
mMyList = activity.updateListInFragment();
}