我正在尝试将我的对象产品从活动B发送到活动A,当应用关闭时没有任何错误消息:
活动A:
public class MainActivity extends AppCompatActivity {
Intent addManualProduct;
TextView name;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (TextView) findViewById(R.id.tv_name);
img = (ImageView) findViewById(R.id.iv_product);
addManualProduct = new Intent(this, Main2Activity.class);
setTitle("ACTIVITY A");
Button openB = (Button) findViewById(R.id.bt_OpenActB);
openB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(addManualProduct, 2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2 && resultCode == RESULT_OK){
Product p = data.getParcelableExtra("product_new");
name.setText(p.getName());
img.setImageBitmap(p.getImg());
}
}
}
活动B:
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
setTitle("ACTIVITY B");
Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.no_image);
Product product = new Product("arroz", img);
Intent toA = new Intent();
toA.putExtra("product_new", product);
setResult(RESULT_OK, toA);
finish();
}
}
产品对象:
public class Product implements Parcelable{
String name;
Bitmap img;
public Product() {}
public Product(String name, Bitmap img){
this.name = name;
this.img = img;
}
protected Product(Parcel in) {
name = in.readString();
img = in.readParcelable(Bitmap.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeParcelable(img, flags);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Product> CREATOR = new Creator<Product>() {
@Override
public Product createFromParcel(Parcel in) {
return new Product(in);
}
@Override
public Product[] newArray(int size) {
return new Product[size];
}
};
public String getName() {
return name;
}
public Bitmap getImg() {
return img;
}
}
如果我只使用parcelable字符串名称,它的工作正常,但是当我尝试将Bitmap关闭时,它会关闭所有应用程序。 我可以说错误是因为Bitmap,但我不知道为什么。
答案 0 :(得分:0)
分析大数据可能会导致应用程序出现问题。我建议不要分配Bitmap
,而是建议分配资源。
public class Product implements Parcelable {
String name;
int bitmapResource;
public Product(String name, int resource){
this.name = name;
this.bitmapResource = resource;
}
}
然后您可以像这样使用Product
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
setTitle("ACTIVITY B");
Product product = new Product("arroz", R.drawable.no_image);
Intent toA = new Intent();
toA.putExtra("product_new", product);
setResult(RESULT_OK, toA);
finish();
}
如果您需要以更动态的方式加载Bitmap
,可以将Uri
存储到Bitmap
,以便可以从原始资源,资源或来自服务。
答案 1 :(得分:0)
你传递的位图可能太大了。 所以尝试压缩位图。 请尝试以下示例: -
http://www.android-examples.com/compress-bitmap-image-in-android-and-reduce-image-size/