Android getExtra()返回空对象
在我的android应用程序中,我试图在两个活动之间传递class对象,但在另一个活动中,我总是得到null 这是我的密码: 要传递数据:Android getExtra()返回空对象,android,android-intent,android-activity,Android,Android Intent,Android Activity,在我的android应用程序中,我试图在两个活动之间传递class对象,但在另一个活动中,我总是得到null 这是我的密码: 要传递数据: public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { try { Products mCurrentProduct = (Products) adapterView.getAdapter().getItem(i);
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
try {
Products mCurrentProduct = (Products) adapterView.getAdapter().getItem(i);
Intent mProductDescription = new Intent(getBaseContext(), Activity_ProductDescription.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(GlobalStrings.EXTRA_MESSAGE_DATA, mCurrentProduct);
mProductDescription.putExtras(mBundle);
if (mProductDescription != null)
startActivity(mProductDescription);
}
catch (Exception e)
{
Log.d("Selection erro :",e.getMessage());
}
}
我的可分配班级:
public class Products implements Parcelable {
public String productName;
public double productPrice;
public String productSize;
public String productWeight;
public String productDescription;
public String brandName;
public byte[] productImage;
public String seller;
public Products(String productName, double productPrice, String productSize, String productWeight,
String productDescription, String brandName, byte[] productImage, String seller) {
this.productName = productName;
this.productPrice = productPrice;
this.productSize = productSize;
this.productWeight = productWeight;
this.productDescription = productDescription;
this.brandName = brandName;
this.productImage = productImage;
this.seller = seller;
}
public String getProductName() {
return productName;
}
public double getProductPrice() {
return productPrice;
}
public String getProductSize() {
return productSize;
}
public String getProductWeight() {
return productWeight;
}
public String getProductDescription() {
return productDescription;
}
public String getBrandName() {
return brandName;
}
public byte[] getProductImage() {
return productImage;
}
public String getSeller() {
return seller;
}
private Products(Parcel p) {
this.productName = p.readString();
this.productPrice = p.readDouble();
this.productSize = p.readString();
this.productWeight = p.readString();
this.productDescription = p.readString();
this.brandName = p.readString();
p.readByteArray(productImage);
this.seller = p.readString();
}
public static final Creator<Products> CREATOR = new Creator<Products>() {
@Override
public Products createFromParcel(Parcel parcel) {
return new Products(parcel);
}
@Override
public Products[] newArray(int i) {
return new Products[i];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(productName);
parcel.writeString(productSize);
parcel.writeString(productWeight);
parcel.writeString(productDescription);
parcel.writeString(brandName);
parcel.writeString(seller);
parcel.writeByteArray(productImage);
parcel.writeDouble(productPrice);
}
}
公共类产品实现可包裹{
公共字符串产品名称;
公共产品价格;
公共字符串大小;
公共字符串权重;
公共字符串产品描述;
公共字符串品牌名称;
公共字节图像;
公共字符串销售商;
公共产品(String productName、double productPrice、String productSize、String productWeight、,
字符串productDescription,字符串brandName,字节[]productImage,字符串卖方){
this.productName=productName;
this.productPrice=productPrice;
this.productSize=productSize;
this.productWeight=productWeight;
this.productDescription=productDescription;
this.brandName=品牌名称;
this.productImage=productImage;
this.seller=卖方;
}
公共字符串getProductName(){
返回产品名称;
}
公共双getProductPrice(){
退货价格;
}
公共字符串getProductSize(){
返回产品大小;
}
公共字符串getProductWeight(){
返回产品重量;
}
公共字符串getProductDescription(){
退货产品说明;
}
公共字符串getBrandName(){
返回品牌名称;
}
公共字节[]getProductImage(){
返回产品图像;
}
公共字符串getSeller(){
退货卖家;
}
私人产品(p包){
this.productName=p.readString();
this.productPrice=p.readDouble();
this.productSize=p.readString();
this.productWeight=p.readString();
this.productDescription=p.readString();
this.brandName=p.readString();
p、 readByteArray(产品图像);
this.seller=p.readString();
}
公共静态最终创建者=新创建者(){
@凌驾
公共产品createFromParcel(包裹){
退回新产品(包裹);
}
@凌驾
公共产品[]新阵列(int i){
退回新产品[i];
}
};
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效书面包裹(包裹,内部i){
parcel.writeString(productName);
包裹。写线(productSize);
包裹的重量(产品重量);
包裹书面记录(产品描述);
包裹书写(品牌名称);
包裹书面记录(卖方);
parcel.writeByteArray(productImage);
包裹。可冲销(产品价格);
}
}
为listview准备适配器的代码:
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("GetProductsResult");
for ( int i=0; i < jsonArray.length();i++ )
{
JSONObject jsonObjectArr = jsonArray.getJSONObject(i);
productsList.add(new Products(
jsonObjectArr.getString("ProductName"),
jsonObjectArr.getDouble("ProductPrice"),
jsonObjectArr.getString("ProductSize"),
jsonObjectArr.getString("ProductWeight"),
jsonObjectArr.getString("ProductDescription"),
jsonObjectArr.getString("BrandName"),
jsonObjectArr.getString("ProductImage").getBytes(),
jsonObjectArr.getString("Seller")));
}
ArrayAdapter<Products> adapter = new ProductListItemAdapters(this,R.layout.list_products,productsList);
mListViewProductList = (ListView) findViewById(R.id.listViewProductList);
mListViewProductList.setAdapter(adapter);
mListViewProductList.setOnItemClickListener(this);
JSONObject JSONObject=新的JSONObject;
JSONArray JSONArray=jsonObject.getJSONArray(“GetProductsResult”);
for(int i=0;i
适配器类
public class ProductListItemAdapters extends ArrayAdapter<Products> {
Context mainContext;
List<Products> productList;
int resourceId;
public ProductListItemAdapters(Context context, int resource, List<Products> objects) {
super(context, resource, objects);
mainContext = context;
productList = objects;
resourceId = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
try {
if (itemView == null) {
LayoutInflater inflater = (LayoutInflater) mainContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.list_products, parent, false);
}
Products prod = productList.get(position);
TextView txtViewSeller = (TextView) itemView.findViewById(R.id.textView_productSeller);
txtViewSeller.setText(prod.getSeller());
TextView txtViewBrand = (TextView) itemView.findViewById(R.id.textView_productBrand);
txtViewBrand.setText(prod.getBrandName());
TextView txtViewProduct = (TextView) itemView.findViewById(R.id.textView_productName);
txtViewProduct.setText(prod.getProductName());
TextView txtViewDesc = (TextView) itemView.findViewById(R.id.textView_productDesc);
txtViewDesc.setText(prod.getProductDescription());
TextView txtViewPrice = (TextView) itemView.findViewById(R.id.textView_productPrice);
txtViewPrice.setText(String.valueOf(prod.getProductPrice()));
}
catch(Exception e) {
Log.d("err", e.toString() );}
return itemView;
}
公共类ProductListItemAdapters扩展了ArrayAdapter{
语境主语境;
列出产品清单;
智力资源ID;
公共产品ListItemAdapters(上下文上下文、int资源、列表对象){
超级(上下文、资源、对象);
mainContext=上下文;
productList=对象;
resourceId=资源;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图项视图=转换视图;
试一试{
如果(itemView==null){
LayoutFlater充气器=(LayoutFlater)主上下文
.getSystemService(上下文布局\充气机\服务);
itemView=充气机。充气(R.layout.list\u产品,父项,false);
}
prod=productList.get(位置);
TextView txtViewSeller=(TextView)itemView.findViewById(R.id.TextView\u productSeller);
txtViewSeller.setText(prod.getSeller());
TextView txtViewBrand=(TextView)itemView.findViewById(R.id.TextView\u productBrand);
txtViewBrand.setText(prod.getBrandName());
TextView txtViewProduct=(TextView)itemView.findViewById(R.id.TextView\u productName);
txtViewProduct.setText(prod.getProductName());
TextView txtViewDesc=(TextView)itemView.findViewById(R.id.TextView\u productDesc);
txtViewDesc.setText(prod.getProductDescription());
TextView txtViewPrice=(TextView)itemView.findViewById(R.id.TextView\u productPrice);
txtViewPrice.setText(String.valueOf(prod.getProductPrice());
}
捕获(例外e){
Log.d(“err”,例如toString());}
返回项目视图;
}
}
我做错什么了吗 不是回答你的实际问题,而是你犯了一个错误。读取包裹时,写入包裹的字段顺序必须相同 像 但在阅读时
this.productName = p.readString();
this.productPrice = p.readDouble();
this.productSize = p.readString();
与序列不匹配。根据Android建议,我将始终建议使用
Parcelable
包裹
和包裹
都很快,但它的文档中说你不能使用
// You followed this sequesnce for writting
parcel.writeString(productName);
parcel.writeString(productSize);
parcel.writeString(productWeight);
parcel.writeString(productDescription);
parcel.writeString(brandName);
parcel.writeString(seller);
parcel.writeByteArray(productImage);
parcel.writeDouble(productPrice);
this.productName = p.readString();
this.productPrice = p.readDouble();
this.productSize = p.readString();
public class Products implements Serializable{
//write all your getter/setter methods here
}
getIntent().getSerializableExtra("Key");