parcel-value-error when using parcelabe custom-object-array

Put your problem here if it does not fit any of the other categories.

parcel-value-error when using parcelabe custom-object-array

Postby mj.devacc » Thu Feb 09, 2012 3:51 pm

Hey @ all!

Short overview about my problem:
I've got a class "Garden" which implements "Parcelable". This class has an Plants-Array "plants" (from my Parcelable class Plant).
That plants[] looks like that: {Plant plant1, Plant plant2, Flower plant3, Flower plant4}.
Flower is a subclass of plant and has its special orders for Parceling.
The problem is:
If i want the values of Flower plant4, i get nonsense values.
This always happens with the SECOND subclass-object in the array.
Doesn't make sense to me?!

Here the codes ;)

EDIT: Some more details on the errors:
To check the problems I System.out.printed different values of plant4.
Thats the result:
Double: 3.0E-323 instead of 1294735.12407523
Long: 28429475166027874 instead of 6
String: empty String (not null) instead of a word
Integer: some big cr*p instead of 5

So I think the heap-adresses aren't correct? That's why the String (an real object) doesn't has any content anymore (but why it isn't null?) and the primitive data types just point at wrong numbers?

EDIT 2:
There's a class cast exception thrown when I try to cast a Flower-Object from the Plant[]-Array like that:

Flower flower = (Flower) garden.plants[3];


Code: Select all
package com.my.package;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ParceltesterActivity extends Activity {
   
   Button b;
   Garden garden;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        b = (Button)findViewById(R.id.button);
        b.setOnClickListener(new OnClickListener() {
         
         public void onClick(View v) {
            
            afterClick();
         }
      });
       
        garden = new Garden();
       
    }
   
    public void afterClick(){
       Intent intent = new Intent(this, SecondActivity.class);
       intent.putExtra("garden", garden);
       startActivity(intent);
    }
   
}


Code: Select all
package com.my.package;

import android.app.Activity;
import android.os.Bundle;

public class SecondActivity extends Activity {

   Garden garden;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      
      garden = getIntent().getParcelableExtra("garden");
      
      System.out.println(garden.plants[0].height);
      System.out.println(garden.plants[1].height);
      System.out.println(garden.plants[2].height);
      System.out.println(garden.plants[3].height);
   }
   
   
}


Code: Select all
package com.my.package;

import android.os.Parcel;
import android.os.Parcelable;

public class Garden implements Parcelable{

   int numberOfPlants;
   Plant[] plants;
   
   public Garden() {
      numberOfPlants = 4;
      plants = new Plant[numberOfPlants];
      plants[0] = new Plant(5);
      plants[1] = new Plant(7);
      plants[2] = new Flower(5,"blue");
      plants[3] = new Flower(4, "green");
   }
   
   public Garden(Parcel in) {
      numberOfPlants = in.readInt();
      plants = new Plant[numberOfPlants];
      in.readTypedArray(plants, Plant.CREATOR);
      
   }
   
   public int describeContents() {
      return 0;
   }

   
   public void writeToParcel(Parcel dest, int flags) {
      dest.writeInt(numberOfPlants);
      dest.writeTypedArray(plants, flags);
   }
   
   public static final Parcelable.Creator<Garden> CREATOR = new Parcelable.Creator<Garden>() {

      public Garden createFromParcel(Parcel source) {
         return new Garden(source);
      }

      public Garden[] newArray(int size) {
         return new Garden[size];
      }
   };

   

}


Code: Select all
package com.my.package;


import android.os.Parcel;
import android.os.Parcelable;

public class Plant implements Parcelable {

   public long height;
   
   public Plant(long height) {
      this.height = height;
   }
   
   protected Plant(Parcel in) {
      height = in.readLong();
   }
   
   public int describeContents() {
      return 0;
   }

   public void writeToParcel(Parcel dest, int flags) {
      dest.writeLong(height);
      
   }
   
   public static final Parcelable.Creator<Plant> CREATOR = new Parcelable.Creator<Plant>() {

      public Plant createFromParcel(Parcel source) {
         return new Plant(source);
      }

      public Plant[] newArray(int size) {
         return new Plant[size];
      }
   };

}


Code: Select all
package com.my.package;

import android.os.Parcel;
import android.os.Parcelable;

public class Flower extends Plant {

   String color;
   
   public Flower(long height, String color) {
      super(height);
      this.color = color;
      // TODO Auto-generated constructor stub
   }

   private Flower(Parcel in) {
      super(in);
      color = in.readString();
   }
   
   @Override
   public void writeToParcel(Parcel dest, int flags) {
         super.writeToParcel(dest, flags);
         dest.writeString(color);
   }
   
   public static final Parcelable.Creator<Flower> CREATOR = new Parcelable.Creator<Flower>() {

      public Flower createFromParcel(Parcel source) {
         return new Flower(source);
      }

      public Flower[] newArray(int size) {
         return new Flower[size];
      }
   };
}


So, any ideas?
mj.devacc
Freshman
Freshman
 
Posts: 2
Joined: Fri Jan 20, 2012 9:15 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 14 guests