当应用程序从此活动关闭时,崩溃并显示错误 - “包裹:无法编组值...”
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当应用程序从此活动关闭时,崩溃并显示错误 - “包裹:无法编组值...”相关的知识,希望对你有一定的参考价值。
我是android编程的初学者。我已经编写了在viewpager中加载图像(从以前的活动中使用serilizable)。
我面临的问题 - 当用户看到这个视图寻呼机并按下主页按钮关闭应用程序时它崩溃并在我的logcat中显示如下错误。我没有得到我的代码有什么问题。我们将不胜感激。谢谢。
我的Logcat:
java.lang.RuntimeException: Parcel: unable to marshal value extra.Image@30335200 at android.os.Parcel.writeValue(Parcel.java:1343) at android.os.Parcel.writeList(Parcel.java:717) at android.os.Parcel.writeValue(Parcel.java:1290) at android.os.Parcel.writeArrayMapInternal(Parcel.java:644) at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313) at android.os.Bundle.writeToParcel(Bundle.java:1034) at android.os.Parcel.writeBundle(Parcel.java:669) at android.support.v4.app.FragmentState.writeToParcel(FragmentState.java:120) at android.os.Parcel.writeTypedArray(Parcel.java:1197) at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:617) at android.os.Parcel.writeParcelable(Parcel.java:1363)
我之前调用此片段的活动的代码:
List<Image> images = new ArrayList<Image>();
images.add(new Image(data_list.get(arg1).getImage().get(0).toString()));//here my images list array gets added in images array
Bundle bundle = new Bundle();
bundle.putSerializable("images", (Serializable) images);
bundle.putInt("position", 0);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
SlideshowDialogFragment newFragment = SlideshowDialogFragment.newInstance();
newFragment.setArguments(bundle);
newFragment.show(ft, "slideshow");
我的图像模型:
public class Image {
private String Image;
private String title;
private String id;
public Image() {
}
public Image(String image, String title, String id) {
Image = image;
this.title = title;
this.id = id;
}
public Image(String image) {
Image = image;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
我的SlideShowDialogFragment.java代码:
public class SlideshowDialogFragment extends DialogFragment {
private String TAG = SlideshowDialogFragment.class.getSimpleName();
private ArrayList<Image> images;
private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
private TextView lblCount, lblTitle, lblDate;
// page change listener
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
displayMetaInfo(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
};
private int selectedPosition = 0;
public static SlideshowDialogFragment newInstance() {
SlideshowDialogFragment f = new SlideshowDialogFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_image_slider, container, false);
viewPager = (ViewPager) v.findViewById(R.id.viewpager);
lblCount = (TextView) v.findViewById(R.id.lbl_count);
lblTitle = (TextView) v.findViewById(R.id.title);
lblDate = (TextView) v.findViewById(R.id.date);
images = (ArrayList<Image>) getArguments().getSerializable("images");
selectedPosition = getArguments().getInt("position");
Log.e(TAG, "position: " + selectedPosition);
Log.e(TAG, "images size: " + images.size());
myViewPagerAdapter = new MyViewPagerAdapter();
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
setCurrentItem(selectedPosition);
return v;
}
private void setCurrentItem(int position) {
viewPager.setCurrentItem(position, false);
displayMetaInfo(selectedPosition);
}
private void displayMetaInfo(int position) {
lblCount.setText((position + 1) + " of " + images.size());
Image image = images.get(position);
lblTitle.setText(image.getTitle());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
// adapter
public class MyViewPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
public MyViewPagerAdapter() {
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.image_fullscreen_preview, container, false);
ImageView imageViewPreview = (ImageView) view.findViewById(R.id.image_preview);
Image image = images.get(position);
Glide.with(getActivity()).load(ConfigClass.BASE_URL + image.getImage())
.thumbnail(0.5f)
.crossFade()
.placeholder(R.drawable.godarpan_logo)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageViewPreview);
container.addView(view);
return view;
}
@Override
public int getCount() {
return images.size();
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view == ((View) obj);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
嗨,请使用下面的代码其他是相同的。
public class Image implements Serializable
{
// your code..
}
在Android中使用Parcelable而不是serializable来将数据从一个活动发送到另一个活动。
请按以下链接查看Parcelable:
https://developer.android.com/reference/android/os/Parcelable.html
因为Parcelable是Android的概念,其中Serializable是Java的概念。
希望能帮助到你。
您必须实现Serializable,但最好实现Parcelable,因为它比Serializable更快更有效,因为它是Android特定的接口。
以上是关于当应用程序从此活动关闭时,崩溃并显示错误 - “包裹:无法编组值...”的主要内容,如果未能解决你的问题,请参考以下文章
当使用 webview 启动活动时,应用程序崩溃并显示“致命信号 5 (SIGTRAP),tid 13058 (Chrome_InProcRe) 中的代码 1”