可包裹更改Android上的资源id

可包裹更改Android上的资源id,android,android-activity,android-resources,parcelable,Android,Android Activity,Android Resources,Parcelable,我有一个产品类,它是可包裹的。此类具有一些图像,这些图像路径保存在strings.xml中。我已将产品对象添加到另一个活动,但图像资源已更改。所以,我得到了ResourcesNotFoundExceptions 这里是mystrings.xml <string-array name="prod_images"> <item>@drawable/sample_product_one</item> <item>@drawable/sa

我有一个产品类,它是可包裹的。此类具有一些图像,这些图像路径保存在strings.xml中。我已将产品对象添加到另一个活动,但图像资源已更改。所以,我得到了ResourcesNotFoundExceptions

这里是mystrings.xml

 <string-array name="prod_images">
    <item>@drawable/sample_product_one</item>
    <item>@drawable/sample_product_two</item>
    <item>@drawable/sample_product_three</item>
    <item>@drawable/sample_product_four</item>
</string-array>
我使用typedarray获取图像资源

private void getProductResources() {
    typedArrayImages = getActivity().getResources().obtainTypedArray(R.array.prod_images);
    prodTitles = getActivity().getResources().getStringArray(R.array.prod_titles);
    prodPrices = getActivity().getResources().getStringArray(R.array.prod_prices);
}

private void getProductList() {
    for (int i = 0; i < 4; i++) {
        int rand_index = random.nextInt(4);
        Product product = new Product();
        product.setProductImage(typedArrayImages.getResourceId(rand_index, -1));
        product.setProductPrice(prodPrices[rand_index]);
        product.setProductTitle(prodTitles[rand_index]);
        product.setProductId(prodIds[rand_index]);
        this.product.add(product);
    }
    typedArrayImages.recycle();
    mainPageAdapter.notifyDataSetChanged();
}
我在设置图像资源时遇到ResourceNotFoundException。我可以在单击图像前后记录资源图像。图像资源会发生变化

    productTitle.setText(product.getProductTitle());
    productCost.setText(product.getProductPrice());
    //Different image resources
    Log.i("PRODUCT", product.getProductImage() + "");
    //ResourcesNotFoundExceptions
    productImg.setImageResource(product.getProductImage());

你必须按照同样的顺序读和写

你写了两个整数,然后是两个字符串

dest.writeInt(this.productId);
dest.writeInt(this.productImage);
dest.writeString(this.productTitle);
dest.writeString(this.productPrice);
但是你读了两个字符串,然后是两个整数

this.productTitle = in.readString();
this.productPrice = in.readString();
this.productId = in.readInt();
this.productImage = in.readInt();

你必须按照同样的顺序读和写

你写了两个整数,然后是两个字符串

dest.writeInt(this.productId);
dest.writeInt(this.productImage);
dest.writeString(this.productTitle);
dest.writeString(this.productPrice);
但是你读了两个字符串,然后是两个整数

this.productTitle = in.readString();
this.productPrice = in.readString();
this.productId = in.readInt();
this.productImage = in.readInt();