我的应用包含 MainActivity ,其中包含选项卡式视图,并为每个标签托管不同的片段。此机制通过扩展FragmentPageAdapter的SectionPageAdapter工作。
其中一个标签显示GeneralSearchFragment
public class GeneralSearchFragment extends Fragment {
public GeneralSearchFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
final ConstraintLayout constraintLayout = (ConstraintLayout) inflater.inflate(R.layout.fragment_searchtab, container, false);
((ImageView)constraintLayout.findViewById(R.id.searchByName)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(getActivity(), FreeSearchActivity.class);
startActivity(intent);
}
});
return constraintLayout;
}
}
当我点击图片时,我的代码应该启动另一个不同的Activity。在我的代码中,我没有将任何内容传递给Activity,因此我不知道序列化错误的来源。我唯一一次序列化的东西是当我通过一个包将一个项目列表传递给一个片段时。但这与这里发生的事情有什么关系呢?
非常感谢
顺便说一句,我理解logcat指出的问题,如果我实际上是在尝试序列化它。我序列化该对象(实际上包含一个drawable)的唯一一次是在另一个片段上,它工作正常。它没有给我任何错误。可绘制的btw来自资源
logcat的
12-26 08:17:52.853 5129-5129/com.chronocrops.chronocropps E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.chronocrops.chronocropps, PID: 5129
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.chronocrops.chronocropps.Model.Crop)
at android.os.Parcel.writeSerializable(Parcel.java:1822)
at android.os.Parcel.writeValue(Parcel.java:1770)
at android.os.Parcel.writeList(Parcel.java:926)
at android.os.Parcel.writeValue(Parcel.java:1719)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:838)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542)
at android.os.Bundle.writeToParcel(Bundle.java:1232)
at android.os.Parcel.writeBundle(Parcel.java:878)
at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:157)
at android.os.Parcel.writeTypedArray(Parcel.java:1462)
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:617)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at android.os.Parcel.writeValue(Parcel.java:1697)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:838)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542)
at android.os.Bundle.writeToParcel(Bundle.java:1232)
at android.app.IActivityManager$Stub$Proxy.activityStopped(IActivityManager.java:4604)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3934)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.io.NotSerializableException: android.graphics.drawable.BitmapDrawable
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1233)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1597)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1558)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1481)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1227)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at android.os.Parcel.writeSerializable(Parcel.java:1817)
at android.os.Parcel.writeValue(Parcel.java:1770)
at android.os.Parcel.writeList(Parcel.java:926)
at android.os.Parcel.writeValue(Parcel.java:1719)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:838)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542)
at android.os.Bundle.writeToParcel(Bundle.java:1232)
at android.os.Parcel.writeBundle(Parcel.java:878)
at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:157)
at android.os.Parcel.writeTypedArray(Parcel.java:1462)
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:617)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at android.os.Parcel.writeValue(Parcel.java:1697)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:838)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542)
at android.os.Bundle.writeToParcel(Bundle.java:1232)
at android.app.IActivityManager$Stub$Proxy.activityStopped(IActivityManager.java:4604)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3934)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
编辑:
/ ** *一个{@link FragmentPagerAdapter},返回对应的片段 *其中一个部分/标签/页面。 * / 公共类SectionsPagerAdapter扩展了FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
// Mudar o retorno do getItem de acordo com a position
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
if(position == 0){
return new CalendarFragment();
}else if(position == 1){
Bundle bundle = new Bundle();
bundle.putSerializable("crops",crops);
CropListFragment cropListFragment = new CropListFragment();
cropListFragment.setArguments(bundle);
return cropListFragment;
}else if(position == 2){
return new GeneralSearchFragment();
}
// Should'nt reach here
return null;
}
裁剪示例:
Crop abacaxi = new Crop("Harvest begins in 10th of June",
getResources().getDrawable(R.drawable.abacax1),
"Marvila",
"Ananas comosus");
裁剪对象
public class Crop implements Serializable {
private String specieHarvest;
private Drawable specieImage;
private String specieLocation;
private String specieName;
public Crop(String specieHarvest, Drawable specieImage, String specieLocation, String specieName) {
this.specieHarvest = specieHarvest;
this.specieImage = specieImage;
this.specieLocation = specieLocation;
this.specieName = specieName;
}
public String getSpecieHarvest() {
return specieHarvest;
}
public Drawable getSpecieImage() {
return specieImage;
}
public String getSpecieLocation() {
return specieLocation;
}
public String getSpecieName() {
return specieName;
}
}
答案 0 :(得分:0)
所以我有一个问题的答案。这个答案基于这篇文章给出的答案:
Why is it possible to pass a Drawable trough a Bundle from one Activity to a Fragment?
答案很简单,因为将对象作为参数传递给Fragment
时不会导致它被Marshled。这意味着除非Fragment
被销毁,否则不会进行序列化/反序列化。在这种情况下,当我尝试开始一个新的Activity
时,他就被摧毁了。我认为Drawable
是可序列化的,因为当我进入Fragment
时我没有收到任何错误,我知道我在处理相同的对象(引用)本身。