![](/img/trans.png)
[英]intent.getExtra returns null while passing data from activity to BroadcastReceiver
[英]intent.getExtra() returns null object
我試圖在我的android應用程序中的兩個活動之間傳遞類對象,但是我總是在另一個活動上獲得null。
這是我的代碼:
傳遞數據:
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());
}
}
獲取數據:
Intent mIntent = getIntent();
Bundle mBundleData = mIntent.getExtras();
Products mCurrentProduct = (Products) mBundleData.getParcelable(EXTRA_MESSAGE_DATA);
Products p = (Products) getIntent().getParcelableExtra(EXTRA_MESSAGE_DATA); // p is always null here.
我的寓言類:
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);
}
}
代碼為列表視圖准備適配器:
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);
轉接器類別
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;
}
}
我做錯什么了嗎?
不是回答您的實際問題,而是您犯了一個錯誤。 閱讀時要寫入包裹的字段順序必須相同。
喜歡
// 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();
與序列不匹配。
根據Android建議,我將始終建議使用Parcelable
。
Parcel
和Parcelable
速度很快,但其文檔指出您不得將其用於存儲的通用序列化,因為實現的方法因Android版本不同而異(即,操作系統更新可能會破壞依賴它的應用程序)
簡單的實現Serializable接口。 它簡單易用。 就像是:
public class Products implements Serializable{
//write all your getter/setter methods here
}
並以您意圖的簡單putExtra()
方法發送對象,並將其作為
getIntent().getSerializableExtra("Key");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.