2

пытаюсь передать из одного активити в другой List

@Generated("org.jsonschema2pojo")
public class ExcuseCategory implements Parcelable {

@SerializedName("id")
@Expose
private String id;
@SerializedName("categoryName")
@Expose
private String categoryName;
@SerializedName("categoryDescription")
@Expose
private String categoryDescription;
@SerializedName("allExcuses")
@Expose
private List<Excuse> allExcuses;

public static final Parcelable.Creator<ExcuseCategory> EXCUSE_CATEGORY_CREATOR = new Creator<ExcuseCategory>() {

    @Override
    public ExcuseCategory createFromParcel(Parcel source) {
        return new ExcuseCategory(source);
    }

    @Override
    public ExcuseCategory[] newArray(int size) {
        return new ExcuseCategory[size];
    }
};

public ExcuseCategory(Parcel in) {
    setId(in.readString());
    setCategoryName(in.readString());
    setCategoryDescription(in.readString());
    setAllExcuses(in.createTypedArrayList(Excuse.EXCUSE_CREATOR));
}

/**
 * @return The id
 */
public String getId() {
    return id;
}

/**
 * @param id The id
 */
public void setId(String id) {
    this.id = id;
}

/**
 * @return The categoryName
 */
public String getCategoryName() {
    return categoryName;
}

/**
 * @param categoryName The categoryName
 */
public void setCategoryName(String categoryName) {
    this.categoryName = categoryName;
}

/**
 * @return The categoryDescription
 */
public String getCategoryDescription() {
    return categoryDescription;
}

/**
 * @param categoryDescription The categoryDescription
 */
public void setCategoryDescription(String categoryDescription) {
    this.categoryDescription = categoryDescription;
}

/**
 * @return The allExcuses
 */
public List<Excuse> getAllExcuses() {
    return allExcuses;
}

/**
 * @param allExcuses The allExcuses
 */
public void setAllExcuses(List<Excuse> allExcuses) {
    this.allExcuses = allExcuses;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(getId());
    dest.writeString(getCategoryName());
    dest.writeString(getCategoryDescription());
    dest.writeTypedList(getAllExcuses());
 }
}

в нем есть List

@Generated("org.jsonschema2pojo")
public class Excuse implements Parcelable {

@SerializedName("excuseTitle")
@Expose
private String excuseTitle;
@SerializedName("excuseContent")
@Expose
private String excuseContent;

public static final Creator<Excuse> EXCUSE_CREATOR = new Creator<Excuse>() {
    @Override
    public Excuse createFromParcel(Parcel source) {
        return new Excuse(source);
    }

    @Override
    public Excuse[] newArray(int size) {
        return new Excuse[size];
    }
};

public Excuse(Parcel source) {
    setExcuseTitle(source.readString());
    setExcuseContent(source.readString());
}

/**
 *
 * @return
 * The excuseTitle
 */
public String getExcuseTitle() {
    return excuseTitle;
}

/**
 *
 * @param excuseTitle
 * The excuseTitle
 */
public void setExcuseTitle(String excuseTitle) {
    this.excuseTitle = excuseTitle;
}

/**
 *
 * @return
 * The excuseContent
 */
public String getExcuseContent() {
    return excuseContent;
}

/**
 *
 * @param excuseContent
 * The excuseContent
 */
public void setExcuseContent(String excuseContent) {
    this.excuseContent = excuseContent;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(getExcuseTitle());
    dest.writeString(getExcuseContent());
}
}

передаю так

            Intent intent = new Intent(getActivity(), ExcuseListActivity.class);
        if (excuseCategoryList != null) {
            intent.putParcelableArrayListExtra("allCategories",    excuseCategoryList);
        } else {
            Log.i(TAG, "onPostExecute: excuseCategoryList = null");
        }
        startActivity(intent);

а при получении
excuseCategoryList=getActivity().getIntent().getParcelableArrayListExtra("allCategories");

вылетает с криками

08-25 18:33:22.021 2391-2391/com.newakkoff.excuse E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: com.newakkoff.excuse, PID: 2391
                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.newakkoff.excuse/com.newakkoff.excuse.activities.ExcuseListActivity}: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.newakkoff.excuse.models.ExcuseCategory
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                 Caused by: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.newakkoff.excuse.models.ExcuseCategory
                                                                    at android.os.Parcel.readParcelableCreator(Parcel.java:2415)
                                                                    at android.os.Parcel.readParcelable(Parcel.java:2337)
                                                                    at android.os.Parcel.readValue(Parcel.java:2243)
                                                                    at android.os.Parcel.readListInternal(Parcel.java:2633)
                                                                    at android.os.Parcel.readArrayList(Parcel.java:1914)
                                                                    at android.os.Parcel.readValue(Parcel.java:2264)
                                                                    at android.os.Parcel.readArrayMapInternal(Parcel.java:2592)
                                                                    at android.os.BaseBundle.unparcel(BaseBundle.java:221)
                                                                    at android.os.Bundle.getParcelableArrayList(Bundle.java:832)
                                                                    at android.content.Intent.getParcelableArrayListExtra(Intent.java:5405)
                                                                    at com.newakkoff.excuse.fragments.ExcuseListFragment.onCreateView(ExcuseListFragment.java:36)
                                                                    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                                                                    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                                                                    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
                                                                    at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                                    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:604)
                                                                    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
                                                                    at android.app.Activity.performStart(Activity.java:6253)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                    at android.os.Looper.loop(Looper.java:148) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Правильно ли я делаю Parcelable классов? Как решить эту проблему?

1 ответ 1

2

Судя по ошибке вам надо просто переименовать EXCUSE_CATEGORY_CREATOR и EXCUSE_CREATOR в CREATOR

1
  • 1
    Да вы правы, большое спасибо, не думал что название может быть так важно.
    – newakkoff
    25 авг 2016 в 21:29

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.