簡體   English   中英

活動從Parcelable對象接收到Null

[英]Activity receives Null from Parcelable object

我正在使用可包裹的對象來傳遞意圖。 我的物件看起來像這樣

enter code here public class Recipe實現Parcelable {private List recipe = 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的問題,我所做的一切都正確,並且沒有任何正常工作。

可能的問題是您使用了應用程序上下文: Intent in = new Intent(getApplicationContext()...改為嘗試傳遞活動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM