Parcelable在Intent putExtra

时间:2016-12-29 09:39:34

标签: java android android-intent serialization

我正在构建一个应用程序,我必须通过List<Object>通过Intent

我的代码:

public class ObjectModelWrapper implements Serializable{

    private List<Object> objectModel;

    public ObjectModelWrapper(List<Object> objectModel) {
        this.objectModel = objectModel;
    }

    public List<Object> getObjectModel() {
        return objectModel;
    }
}

以下是我尝试通过Intent的数据:

holder.categoryAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                firstSix = new ArrayList<>();
                newList = new ArrayList<>();
                if (items.get(position).getGames().size() > 12) {
                    firstSix.addAll(items.get(position).getGames().subList(0, 6));
                    newList.add(firstSix);
                    newList.add(items.get(position).getBannerImage());
                    newList.add(items.get(position).getGames().subList(6, items.get(position).getGames().size()));
                }else if (items.get(position).getGames().size() > 8 && items.get(position).getGames().size() < 12) {
                    firstSix.addAll(items.get(position).getGames().subList(0, 6));
                    newList.add(firstSix);
                    newList.add(items.get(position).getBannerImage());
                    newList.add(items.get(position).getGames().subList(6, items.get(position).getGames().size()));
                }else if (items.get(position).getGames().size() > 3 && items.get(position).getGames().size() < 8) {
                    firstSix.addAll(items.get(position).getGames().subList(0, 3));
                    newList.add(firstSix);
                    newList.add(items.get(position).getBannerImage());
                    newList.add(items.get(position).getGames().subList(3, items.get(position).getGames().size()));
                }else {
                    firstSix.addAll(items.get(position).getGames().subList(0, 1));
                    newList.add(firstSix);
                    newList.add(items.get(position).getBannerImage());
                    newList.add(items.get(position).getGames().subList(1, items.get(position).getGames().size()));
                }
                ObjectModelWrapper wrapper = new ObjectModelWrapper(newList);

                for (int i=0; i< firstSix.size(); i++){
                    Log.e("GAME NAME FIRST SIX", firstSix.get(i).getName());
                }

                for (int i=0; i< newList.size(); i++){
                    Log.e("GAME NAME NEW LIST", newList.get(i).toString());
                }
                Intent intent = new Intent(context, CategoryInfo.class);
                intent.putExtra("categoryColor", items.get(position).getCategoryLineColor());
                intent.putExtra("categoryBanner", items.get(position).getBannerImage());
                intent.putExtra("categoryName", items.get(position).getCategoryName());
                intent.putExtra("customList", wrapper);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });

newList是:

private transient List<Object> newList;

我知道可能是因为Object尚未实施Serializable。我该如何解决这个问题?

logcat输出:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.realtimegaming.network.models.ObjectModelWrapper)
                                                                                                  at android.os.Parcel.writeSerializable(Parcel.java:1527)
                                                                                                  at android.os.Parcel.writeValue(Parcel.java:1475)
                                                                                                  at android.os.Parcel.writeArrayMapInternal(Parcel.java:724)
                                                                                                  at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
                                                                                                  at android.os.Bundle.writeToParcel(Bundle.java:1157)
                                                                                                  at android.os.Parcel.writeBundle(Parcel.java:764)
                                                                                                  at android.content.Intent.writeToParcel(Intent.java:8687)
                                                                                                  at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3082)
                                                                                                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
                                                                                                  at android.app.Activity.startActivityForResult(Activity.java:4225)
                                                                                                  at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
                                                                                                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
                                                                                                  at android.app.Activity.startActivityForResult(Activity.java:4183)
                                                                                                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
                                                                                                  at android.app.Activity.startActivity(Activity.java:4522)
                                                                                                  at android.app.Activity.startActivity(Activity.java:4490)
                                                                                                  at adapters.LobbyAdapter$1.onClick(LobbyAdapter.java:114)
                                                                                                  at android.view.View.performClick(View.java:5637)
                                                                                                  at android.view.View$PerformClick.run(View.java:22429)
                                                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                               Caused by: java.io.NotSerializableException: java.util.ArrayList$SubList
                                                                                                  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1224)
                                                                                                  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
                                                                                                  at java.util.ArrayList.writeObject(ArrayList.java:734)
                                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                                  at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:977)
                                                                                                  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1536)
                                                                                                  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472)
                                                                                                  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218)
                                                                                                  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1584)
                                                                                                  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1549)
                                                                                                  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472)
                                                                                                  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218)
                                                                                                  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
                                                                                                  at android.os.Parcel.writeSerializable(Parcel.java:1522)
                                                                                                  at android.os.Parcel.writeValue(Parcel.java:1475) 
                                                                                                  at android.os.Parcel.writeArrayMapInternal(Parcel.java:724) 
                                                                                                  at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408) 
                                                                                                  at android.os.Bundle.writeToParcel(Bundle.java:1157) 
                                                                                                  at android.os.Parcel.writeBundle(Parcel.java:764) 
                                                                                                  at android.content.Intent.writeToParcel(Intent.java:8687) 
                                                                                                  at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3082) 
                                                                                                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518) 
                                                                                                  at android.app.Activity.startActivityForResult(Activity.java:4225) 
                                                                                                  at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50) 
                                                                                                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79) 
                                                                                                  at android.app.Activity.startActivityForResult(Activity.java:4183) 
                                                                                                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859) 
                                                                                                  at android.app.Activity.startActivity(Activity.java:4522) 
                                                                                                  at android.app.Activity.startActivity(Activity.java:4490) 
                                                                                                  at adapters.LobbyAdapter$1.onClick(LobbyAdapter.java:114) 
                                                                                                  at android.view.View.performClick(View.java:5637) 
                                                                                                  at android.view.View$PerformClick.run(View.java:22429) 
                                                                                                  at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                                  at android.os.Looper.loop(Looper.java:154) 
                                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6119)

1 个答案:

答案 0 :(得分:0)

为什么在列表中使用Object?尝试为列表的所有对象创建一个基类,并创建继承类以获取详细信息。然后,在每个课程中,您可以实现自己的Parcelable