【问题标题】:Activity receives Null from Parcelable objectActivity 从 Parcelable 对象接收 Null
【发布时间】:2015-11-22 07:38:53
【问题描述】:

我正在使用一个可包裹的对象来传递一个意图。我的对象看起来像这样

enter code herepublic class Recipe 实现 Parcelable{ 私人清单配方=空; 私人清单成分=空; 私有字符串准备;

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 的问题,一切都正确,没有任何东西能正常工作。

【问题讨论】:

标签: android android-intent null parcelable serializable


【解决方案1】:

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

【讨论】:

  • 尝试传递活动是什么意思。要初始化一个意图,你必须给它一个参数 getApplicationContext() 或 getBaseContext()。
  • 我的意思是MainActivity.this,例如
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
相关资源
最近更新 更多