在一個android應用程序中,我嘗試將一個可序列化的對象數組發送到片段中的活動。但是當我在活動中反序列化時,它不斷顯示類拋出異常。ClassCastException在從意圖額外數據中檢索數據時發生
下面是頁面適配器的代碼,其中在圖像視圖上點擊嘗試打開活動。
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);
}
}
這裏是活動的,我想從片段
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);
*/
}
}
得到並行化數據的代碼下面是我使用的序列化模型類的代碼。
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 + "]";
}
}
我已經試過了。即使這樣我也有同樣的錯誤。我不知道爲什麼會發生這種情況。 – darthvish
現在試試這個Fragment使用getActivity()方法返回它的Activty,嘗試使用'_context.getActivity()'或者'getActivity()' –