In an android app I trying to send a serializable object array to an activity from a fragment. But when I deserializing in activity it constantly shows class cast exception.
Here is the code of pager adapter where on image view click trying to open a activity.
public class ItemImageSliderAdapter extends PagerAdapter {
Context _context;
Images[] images;
public ItemImageSliderAdapter(Context _context, Images[] images) {
this._context = _context;
this.images = images;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(_context);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Picasso.with(_context).load(images[position].getMain()).into(imageView);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (images != null) {
fullImageSlider(v, images);
}
}
});
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public int getCount() {
return images.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((ImageView) arg1);
}
public void fullImageSlider(View v, Images[] imageArray) {
Intent intent = new Intent(v.getContext(), FullScreenImageSlider.class);
Bundle bundle = new Bundle();
bundle.putSerializable("data",imageArray);
bundle.putString("flag", "hello");
intent.putExtras(bundle);
v.getContext().startActivity(intent);
}
}
here is the code of activity where I am trying to get deserialized data from fragment
public class FullScreenImageSlider extends Activity {
Images[] images = null;
String flag;
SmartViewPager slidePager;
CirclePageIndicator pageIndicator;
ItemImageSliderAdapter slideAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().getExtras().getSerializable("data") != null) {
flag = getIntent().getExtras().getString("flag");
try {
images = (Images[]) getIntent().getExtras().getSerializable(
"data");
} catch (ClassCastException e) {
Log.d("ex", "class cast exception");
}
}
setContentView(R.layout.full_screen_slider);
Log.d("flag", flag);
findViews();
}
private void findViews() {
slidePager = (SmartViewPager) findViewById(R.id.vp_slider);
pageIndicator = (CirclePageIndicator) findViewById(R.id.dots);
/*
* slideAdapter = new ItemImageSliderAdapter(this, images);
* slidePager.setAdapter(slideAdapter);
* pageIndicator.setViewPager(slidePager);
*/
}
}
Here is the code of the model class I am using to serialize.
public class Images implements Serializable{
private String id;
private String isdefault;
private String thumb;
private String main;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIsdefault() {
return isdefault;
}
public void setIsdefault(String isdefault) {
this.isdefault = isdefault;
}
public String getThumb() {
return thumb;
}
public void setThumb(String thumb) {
this.thumb = thumb;
}
public String getMain() {
return main;
}
public void setMain(String main) {
this.main = main;
}
@Override
public String toString() {
return "ClassPojo [id = " + id + ", isdefault = " + isdefault
+ ", thumb = " + thumb + ", main = " + main + "]";
}
}
Use the _context
variable to start intent
public void fullImageSlider(View v, Images[] imageArray) {
Intent intent = new Intent(_context.getActivity(), FullScreenImageSlider.class);
Bundle bundle = new Bundle();
bundle.putSerializable("data",imageArray);
bundle.putString("flag", "hello");
intent.putExtras(bundle);
_context.getActivity().startActivity(intent);
}
You are receiving ClassCastException cause you object from extras is not Serializable
(it is Serializable[]
) while you are trying to get it with getSerializable
method. You should use Parcelable
instead of Serializable
for Images
. Then you can just use bundle.putParcelableArray(key, array);
to pass your data over intent.
请检查您的图像导入类是否是使用android.provider.MediaStore.Images而不是您的类
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.