0
@SerializedName("profileName")
private String profileName;
@SerializedName("paused")
private boolean isPaused;
@SerializedName("numberOfDevices")
private int numberOfDevices;

private boolean isSelected;

public Profile(String profileName, Boolean isPaused, int numberOfDevices) {
    this.profileName = profileName;
    this.isPaused = isPaused;
    this.numberOfDevices = numberOfDevices;
}

protected Profile(Parcel in) {
    profileName = in.readString();
    isPaused = (boolean) in.readValue(boolean.class.getClassLoader());
    isSelected = (boolean) in.readValue(boolean.class.getClassLoader());
    numberOfDevices = in.readInt();
}

So in the above code dont know the reason why I am getting classCastExcpetion on isSelected and not in isPaused. Both are having same datatypes and used in a similar way. Tried searching for it still not able to understand the issue. Below are the logs for it.

java.lang.IllegalStateException: Bundle length is not aligned by 4: 9
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3447)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3594)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2146)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7762)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
 Caused by: java.lang.IllegalStateException: Bundle length is not aligned by 4: 9
    at android.os.BaseBundle.readFromParcelInner(BaseBundle.java:1615)
    at android.os.BaseBundle.<init>(BaseBundle.java:143)
    at android.os.Bundle.<init>(Bundle.java:95)
    at android.os.Parcel.readBundle(Parcel.java:2301)
    at android.os.Parcel.readValue(Parcel.java:2946)
    at models.Profile.<init>(Profile.java:27)
    at models.Profile$1.createFromParcel(Profile.java:34)
    at models.Profile$1.createFromParcel(Profile.java:31)
    at android.os.Parcel.readParcelable(Parcel.java:2990)
    at android.os.Parcel.readValue(Parcel.java:2883)
    at android.os.Parcel.readArrayMapInternal(Parcel.java:3261)
    at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:292)
    at android.os.BaseBundle.unparcel(BaseBundle.java:236)
    at android.os.Bundle.getParcelable(Bundle.java:951)

And the weird part is I am using that for a recycler view. If I click on 1st item in recycler view it gives the below IllegalStateException and if I click on any other item I am getting ClassCastException as below

java.lang.Short cannot be cast to java.lang.Boolean
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3447)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3594)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2146)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7762)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
 Caused by: java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.Boolean
    at com.centurylink.ctl_droid_wrap.models.Profile.<init>(Profile.java:27)
    at com.centurylink.ctl_droid_wrap.models.Profile$1.createFromParcel(Profile.java:34)
    at com.centurylink.ctl_droid_wrap.models.Profile$1.createFromParcel(Profile.java:31)
    at android.os.Parcel.readParcelable(Parcel.java:2990)
    at android.os.Parcel.readValue(Parcel.java:2883)
    at android.os.Parcel.readArrayMapInternal(Parcel.java:3261)
    at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:292)
    at android.os.BaseBundle.unparcel(BaseBundle.java:236)
    at android.os.Bundle.getParcelable(Bundle.java:951)
Darpal
  • 352
  • 5
  • 20

2 Answers2

0

You don't have @SerializedName for isSelected. It will read the value of numberOfDevices and assign to isSelected and cause the exception. Add SerializedName for isSelected and the problem will go away.

Hoàn Lê
  • 127
  • 1
  • 4
  • Theres no use of SerializedName for isSelected in my case. – Darpal Feb 07 '20 at 19:07
  • I mean that is why it get exception, because the parcel has no information of it but you force it to read value and assign it to isSelected. You should consider using transient annotation. – Hoàn Lê Feb 07 '20 at 19:17
  • Thanks for checking my code, I found my issue i posted above in answers – Darpal Feb 07 '20 at 19:18
0
 protected Profile(Parcel in) {
    profileName = in.readString();
    isPaused = in.readByte() != 0;
    numberOfDevices = in.readInt();
    isSelected = in.readByte() != 0;
}

I just replaced with above code and it solved the issue. There was some issue in above class.

Darpal
  • 352
  • 5
  • 20