1

Can you tell me how to solve this? It throws a bad array length exception and after debugging, I found this method throws the Exception:

--------------------------My Code-----------------------------------

public class SingleModels implements Parcelable{

    public String name;
    public String[] names ;

    @Override
    public int describeContents() {

        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(name);
        dest.writeStringArray(names);
    }
    public static final Parcelable.Creator<SingleModels> CREATOR = new Creator<SingleModels>() {

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

        @Override
        public SingleModels createFromParcel(Parcel source) {
            SingleModels models = new SingleModels();
            models.name = source.readString();
            models.names = source.createStringArray();
            source.readStringArray(models.names);
            return models;}
    };
}

This is my exception, but so long, I can't upload the image, I cut the information by important:

07-07 09:52:59.319: E/AndroidRuntime(4296): FATAL EXCEPTION: main
07-07 09:52:59.319: E/AndroidRuntime(4296): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.single.parcelable.example/edu.single.parcelable.example.ArrayParcelable_Activity}: java.lang.RuntimeException: bad array lengths
07-07 09:52:59.319: E/AndroidRuntime(4296):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
Alexey Malev
  • 6,408
  • 4
  • 34
  • 52
Single
  • 31
  • 1
  • 5

1 Answers1

2

This is the implementation of readStringArray():

public final void readStringArray(String[] val) {
    int N = readInt();
    if (N == val.length) {
        for (int i=0; i<N; i++) {
            val[i] = readString();
        }
    } else {
        throw new RuntimeException("bad array lengths");
    }
}

So it seems as though the length of your models.names array is the reason it's throwing that error. You need to know the exact size of your array beforehand to create the array so that it doesn't throw the error.

I think createStringArray() suffices, you don't need to call source.readStringArray(models.names); so just delete that and it should work.


I think there may be a language barrier here, so i'll mention the exact code you should have:

@Override
    public SingleModels createFromParcel(Parcel source) {
        SingleModels models = new SingleModels();
        models.name = source.readString();
        models.names = source.createStringArray();
        return models;
}
u3l
  • 3,342
  • 4
  • 34
  • 51
  • public SingleModels createFromParcel(Parcel source) { SingleModels models = new SingleModels(); models.name = source.readString(); models.names = new String[7]; source.readStringArray(models.names); return models; } and I set the StringArray length is OK – Single Jul 07 '14 at 10:03
  • First, try to simply remove the line where you `readStringArray`, I don't think you need it at all. – u3l Jul 07 '14 at 10:05
  • I making createStringArray() at last,and call the readInt(),readInt() is Not Equal Array legth – Single Jul 07 '14 at 10:07
  • Why are you calling `readInt`? All you have to do is remove `readStringArray` and your code should work. – u3l Jul 07 '14 at 10:11
  • that's right !!! thank you answer my question ! – Single Jul 07 '14 at 11:44
  • Awesome, that Helped!!! Thank You very much – Anaximandro Andrade Feb 19 '16 at 13:02