如何在Android中将一个Activity对象转换为另一个Activity?

时间:2013-10-02 12:21:03

标签: android android-intent parcelable

我很难坚持使用Parcelable将对象从一个活动传递到另一个活动但我在行Log.i("Name",""+rcp.getName());处获得空指针异常,您可以在下面的代码中检查此行。 Plz最后检查代码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() 
    {
    }
}

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

2 个答案:

答案 0 :(得分:0)

关于主要活动:

intent.putExtra("Cooking",cook);

然后是第二项活动:

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

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

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

答案 1 :(得分:0)

很少有需要改变的事情。

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

    CookingDataModel cook = new CookingDataModel();

  2. 从意图附加内容中获取Parcelable对象时的Typecast,

    cook = bundle.getParcelable(“Cooking”);

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