简体   繁体   中英

Activity receives Null from Parcelable object

I am using a parcelable object to pass through an intent. My object looks like this

enter code here public class Recipe implements Parcelable{ private List recipe = null; private List ingredients = null; private String preparation;

public Recipe(List<String> recipe, List<String> ingredients,
        String preparation) {
    this.recipe = recipe;
    this.ingredients = ingredients;
    this.preparation = preparation;
}

public Recipe(Parcel source) {

    source.readStringList(recipe);
    source.readStringList(ingredients);
    this.preparation = source.readString();
}

public List<String> getRecipe() {
    return recipe;
}

public List<String> getIngredients() {
    return ingredients;
}

public String getPreparation() {
    return preparation;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeStringList(recipe);
    dest.writeStringList(ingredients);
    dest.writeString(preparation);

}

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

    @Override
    public Recipe[] newArray(int size) {
        return new Recipe[size];
    }

    @Override
    public Recipe createFromParcel(Parcel source) {
        return new Recipe(source);
    }
};}

I put the data in to Intent like this

enter code here @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipe);

    dbMan = new XmlManager();
    try {
        db = dbMan.parse();
    } catch (XmlPullParserException e) {
        System.err.println("XmlPullParserException " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IO Excepion " + e.getMessage());
        e.printStackTrace();
    }

    // get the listview
    expListView = (ExpandableListView) findViewById(R.id.expandableListView1);

    // preparing list data
    prepareListData();

    listAdapter = new MyBaseExpandableListAdapter(this, listDataHeader,
            listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);
    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            sendChildList = listDataChild 
                    .get(listDataHeader.get(groupPosition))
                    .get(childPosition).toString();
            sendRecipe = dbMan.getRecipyByCriteria(db, sendChildList);

            Toast.makeText(getApplicationContext(),
                    "sendet" + sendChildList, Toast.LENGTH_LONG).show();
            Intent in = new Intent(getApplicationContext(),
                    ShowRecipe.class);
            in.putExtra("sendRecipe", sendRecipe);

            startActivity(in);
            return false;
        }
    });
}

Well i debug this Part of code and i know the sendRecipe Object was instantiates and data is correct. so i put this thing in intent and send it to next Activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_recipe);
    findeRecipe = (Recipe)getIntent().getParcelableExtra("sendRecipe");

At this place the Intent receives NULL. It's funny thing because when I send a String or int its receive correctly. I sit here scened day an debug this thing read questions about Parcelable do everything right i thing and nothing works correctly.

可能的问题是您使用了应用程序上下文: Intent in = new Intent(getApplicationContext()...改为尝试传递活动。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM