1

我非常坚持使用 Parcelable 将对象从一个活动传递到另一个活动,但是我在 line 处遇到空指针异常Log.i("Name",""+rcp.getName());,您可以在下面的代码中检查这一行。请在最后检查代码 CookingDataModel 类。

这是对象接收活动的代码

protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// No title to display
        setContentView(R.layout.recipe_ingredient_detail);

        CookingDataModel cook = new CookingDataModel();
        RecipeDataModel rcp;
        ArrayList<IngredientDataModel> ing;

        Bundle bundle  = this.getIntent().getExtras();
        if(bundle!=null)
        cook = bundle.getParcelable("Cooking");

        rcp = cook.getRecipe();
        ing = cook.getIngredientList();


        Log.i("Name",""+rcp.getName());
        Log.i("Decrp",""+rcp.getDescription());
        Log.i("Duration",""+rcp.getPrepTime());
        Log.i("Instructions",""+rcp.getInstructions());

        for(int k = 0; k < ing.size(); k++)
        {
            Log.i("Item Name",""+ing.get(k).getItemName());
            Log.i("Item Amount",""+ing.get(k).getItemAmount());
        }
    }

这是我发送对象的代码CookingDataModel

ListView recepeListView = getListView();
        recepeListView.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) 
            {
                CookingDataModel cook = recpeList.get(position);

                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putParcelable("Cooking",cook);
                intent.putExtras(bundle);
                intent.setClass(RecipeList.this,RecipeIngredientDetail.class);
                startActivity(intent);

            }
        });

这是 CookingDataModel 类的代码。

public class CookingDataModel implements Parcelable{

    private RecipeDataModel recipe = null;
    private ArrayList<IngredientDataModel> ingredientList = null;

    public RecipeDataModel getRecipe() {
        return recipe;
    }
    public void setRecipe(RecipeDataModel recipe) {
        this.recipe = recipe;
    }
    public ArrayList<IngredientDataModel> getIngredientList() {
        return ingredientList;
    }
    public void setIngredientList(ArrayList<IngredientDataModel> ingredientList) {
        this.ingredientList = ingredientList;
    }

    public static final Parcelable.Creator<CookingDataModel> CREATOR = new Parcelable.Creator<CookingDataModel>() 
    {
        public CookingDataModel createFromParcel(Parcel in) 
        {
            return new CookingDataModel(in);
        }

        public CookingDataModel[] newArray(int size) 
        {
            return new CookingDataModel[size];
        }
    };

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void writeToParcel(Parcel arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    public CookingDataModel(Parcel in) {
        // TODO Auto-generated constructor stub

    }

    public CookingDataModel() 
    {
    }
}

请在这方面帮助我,以便我可以继续我的项目。提前致谢。

4

2 回答 2

0

需要改变的东西很少。

  1. 无需创建新对象,它将被覆盖。

    CookingDataModel 厨师 = 新的 CookingDataModel();

  2. 当您从 Intent Extras 中获取 Parcelable 对象时进行类型转换,

    厨师 = bundle.getParcelable("烹饪");

  3. 确保在发送对象时,它具有有效的接收成员。如果您注意到,您可以从 Intent 中获取 CookingDataModel,也可以从该对象中获取 Receipe,但无法从 ReceipeModel 中获取数据。从发送活动中的代码来看,我真的不能说 CookingDataModel.Receipe 是否是一个有效的对象。

于 2013-10-02T12:36:31.413 回答
0

关于主要活动:

intent.putExtra("Cooking",cook);

然后在第二个活动:

getIntent().getExtras().getParcelable("Cooking");

尝试这个。如果您只是发送单个对象,请不要使用 bundle。

您可能需要转换 getIntent().... 部分,我不确定。

于 2013-10-02T12:27:34.323 回答