Activity从Parcelable对象接收Null

时间:2015-08-27 19:05:12

标签: android android-intent null parcelable serializable

我正在使用一个parcelable对象来传递一个intent。我的对象看起来像这样

enter code here公共类Recipe实现了Parcelable {     private List recipe = null;     private List ingredients = null;     私人字符串准备;

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);
    }
};}

我将数据放入Intent,就像这样

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;
        }
    });
}

我调试这部分代码,我知道sendRecipe对象是实例化的,数据是正确的。所以我把这个东西放在意图中并发送到下一个活动

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

在这个地方,Intent收到NULL。这很有趣,因为当我发送一个String或int正确接收它时。我坐在这里一天调试这个东西读了关于Parcelable的问题做的一切正确我的事情,没有什么工作正常。

1 个答案:

答案 0 :(得分:1)

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