将 parcelable 对象传递给新活动返回 null

Posted

技术标签:

【中文标题】将 parcelable 对象传递给新活动返回 null【英文标题】:Passing parcelable object to new activity returns null 【发布时间】:2019-07-01 06:53:12 【问题描述】:

我将一个可打包数据类的对象传递给一个简单字符串的活动 我可以将它放入一个意图并将其传递给另一个活动,但是当我在我的对象中输入一个新的字符串列表时,该对象(现在包括一个列表)没有被传递给其他活动。

这是我的数据类”

package com.example.user.shoppy.models;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class ProductModel implements Parcelable 

String product_name, description, price, currency, location, userId, image, 
product_Id,featuredImage;

List<String> imagesNames;

public ProductModel() 




// this constructor is getting all the string variables
public ProductModel(String product_Id, String product_name, String description, String price, String currency, String userId, String location) 
    this.product_name = product_name;
    this.description = description;
    this.price = price;
    this.currency = currency;
    this.location = location;
    this.userId = userId;
    this.product_Id = product_Id;

// this constructor is getting all the string variables but also an list 
public ProductModel(String product_id, String product_name, String des, String price, String currency, String userId, String location, String featuredImage, List imagesNames) 
    this.product_name = product_name;
    this.description = des;
    this.price = price;
    this.currency = currency;
    this.location = location;
    this.userId = userId;
    this.product_Id = product_id;
    this.featuredImage = featuredImage;
    this.imagesNames = imagesNames;



public String getProduct_Id() 
    return product_Id;


public void setProduct_Id(String product_Id) 
    this.product_Id = product_Id;


public String getImage() 
    return image;


public void setImage(String image) 
    this.image = image;


public String getUserId() 
    return userId;


public void setUserId(String userId) 
    this.userId = userId;


public String getProduct_name() 
    return product_name;


public void setProduct_name(String product_name) 
    this.product_name = product_name;


public String getDescription() 
    return description;


public void setDescription(String description) 
    this.description = description;


public String getPrice() 
    return price;


public void setPrice(String price) 
    this.price = price;


public String getCurrency() 
    return currency;


public void setCurrency(String currency) 
    this.currency = currency;


public String getLocation() 
    return location;


public void setLocation(String location) 
    this.location = location;


public String getFeaturedImage()         return featuredImage;    

public void setFeaturedImage(String featuredImage)         this.featuredImage = featuredImage;    

public List<String> getImagesNames()         return imagesNames;    

public void setImagesNames(List<String> imagesNames)         this.imagesNames = imagesNames;    

@Override
public int describeContents() 
    return 0;


@Override
public void writeToParcel(Parcel dest, int flags) 
    dest.writeString(description);
    dest.writeString(product_name);
    dest.writeString(price);
    dest.writeString(currency);
    dest.writeString(location);
    dest.writeString(userId);
    dest.writeString(image);
    dest.writeString(product_Id);
    dest.writeString(featuredImage);
    dest.writeStringList(imagesNames);


public static final Parcelable.Creator<ProductModel> CREATOR = new Creator<ProductModel>() 
    @Override
    public ProductModel createFromParcel(Parcel source) 
        return new ProductModel(source);
    

    @Override
    public ProductModel[] newArray(int size) 
        return new ProductModel[size];
    
;

private ProductModel(@NonNull Parcel in) 
    description = in.readString();
    product_name = in.readString();
    price = in.readString();
    currency = in.readString();
    location = in.readString();
    userId = in.readString();
    image = in.readString();
    product_Id = in.readString();
    featuredImage=in.readString();
    in.readList(imagesNames,String.class.getClassLoader());


下面是剩下的代码:

  @Override
        public void onClick(View v) 

             Intent intent = new Intent(mContext, ProductSellingActivity.class);
            intent.putExtra("currentProductModel", currentProductModel);
            mContext.startActivity(intent);


        
    );

现在我的意图接收类

 Intent intent=getIntent();
    currentProductModel= (ProductModel) intent.getParcelableExtra(ProductSellingActivity.INTENTNAME);

【问题讨论】:

确保您的密钥currentProductModel在接收活动时名称相同 同理,问题出在列表数据类型上。它与简单的字符串一起工作正常...... 只是想确保数据是否填充到此对象中的currentProductModel。在mContext.startActivity(intent); 行设置断点并检查该对象的值 是的,“currentProductModel”对象具有所有值,但是当它到达下一个活动时,调试器显示意图具有附加值,但没有为其分配值。 【参考方案1】:

虽然在第二个活动键中接收意图应该是相同的。

Intent intent=getIntent();
currentProductModel= (ProductModel) intent.getParcelableExtra("currentProductModel");

试试这个。

【讨论】:

我做了一个全局变量如下,所以键名是一样的。 ` 私有最终静态字符串 INTENTNAME="currentProductModel"; `【参考方案2】:

我认为您的 ProductModel 未在新 Activity 中正确重新创建。

在您的包裹实施中试试这个:

imagesNames = in.readArrayList(ProductModel.class.getClassLoader());

【讨论】:

以上是关于将 parcelable 对象传递给新活动返回 null的主要内容,如果未能解决你的问题,请参考以下文章

如何将包含流的复杂对象传递给活动

如何将不可打包的对象从活动传递到另一个活动?

当我将 Android Location 对象传递给 asyncTask 时,它返回 null

如何将 Fire store ref 对象传递给另一个活动(Kotlin)?

尝试将对象传递给活动时得到nullpointer

将 Firebase 用户对象传递给新组件