0

I'm making a game, and I need to move a custom Object. To do that, I'm using a Parcelable to move data.

However, I'm having problems moving the data, and I don't know why.

This is Parcelable class:

public class Character implements Parcelable {
    public static int day;
    public static int now_food;
    public static int now_water;
    public static int use_food;
    public static int use_water;
    public static int health;
    public static String name;
    public static char status; // d = 병걸림, n = 정상
    public static char status_live; //w = 죽음 l = 생존
    public static String status_water ; // t = 목마름 v = 매우 목마름
    public static String status_food ; // h = 배고픔 v = 매우 배고픔

    public Character() {
    }

    public Character(Parcel in) {
    }

    public Character(int use_food, int use_water, int health, String name, char status) {
        this.status = status;
        this.use_food = use_food;
        this.use_water = use_water;
        this.health = health;
        this.name = name;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(use_food);
        dest.writeInt(use_water);
        dest.writeInt(health);
        dest.writeString(name);
    }

    public void readFromParcel(Parcel in) {
        use_food = in.readInt();
        use_water = in.readInt();
        health = in.readInt();
        name = in.readString();
    }

    public static final Creator<Character> CREATOR = new Creator<Character>() {
        @Override
        public Character createFromParcel(Parcel in) {
            return new Character(in);
        }

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

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

Here's my main code:

character1.setOnClickListener(new Button.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), Character1_Activity.class);
        intent.putExtra("ch1info", ch1);
        startActivity(intent);
    }
});

I move this object with button's click event.

Finally, this is the Activity that wants to get object.

Bundle bundle = getIntent().getExtras();
final Character ch1 = bundle.getParcelable("ch1info");
TextView get_water = (TextView) findViewById(R.id.water_stat);
get_water.setText(ch1.Check_water(ch1));

Why doesn't my code work, and how can I fix it?

halfer
  • 19,824
  • 17
  • 99
  • 186
InhoLee
  • 59
  • 6

2 Answers2

0

Update your Character class as below:

public class Character implements Parcelable {

    public static int day;
    public static int now_food;
    public static int now_water;
    public static int use_food;
    public static int use_water;
    public static int health;
    public static String name;
    public static char status; // d = 병걸림, n = 정상
    public static char status_live; //w = 죽음 l = 생존
    public static String status_water ; // t = 목마름 v = 매우 목마름
    public static String status_food ; // h = 배고픔 v = 매우 배고픔

    public Character() {

    }

    public Character(int use_food, int use_water, int health, String name, char status) {
        this.status = status;
        this.use_food = use_food;
        this.use_water = use_water;
        this.health = health;
        this.name = name;
    }

    public Character(Parcel in) {
        this.use_food = in.readInt();
        this.use_water = in.readInt();
        this.health = in.readInt();
        this.name = in.readString();
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(use_food);
        dest.writeInt(use_water);
        dest.writeInt(health);
        dest.writeString(name);
    }

    public static final Parcelable.Creator<Character> CREATOR = new Parcelable.Creator<Character>() {
        @Override
        public Character createFromParcel(Parcel in) {
            return new Character(in);
        }

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

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

}

Send Character object:

character1.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {

            // Object
            Character ch1 = new Character(27,32,65,"name",'n');

            Intent intent = new Intent(getActivity(), Character1_Activity.class);
            intent.putExtra("ch1info", ch1);
            startActivity(intent);
        }

    });

Retrieve Character object in your Character1_Activity.java:

Bundle bundle = getIntent().getExtras();
final Character ch1 = bundle.getParcelable("ch1info");

Log.d("Success", "Use water: " + ch1.use_water  + "\nUse food:" + ch1.use_food
      + "\nHealth: " + ch1.health + "\nName: " + ch1.name + "\nStatus: " + ch1.status);

OUTPUT:

D/Success: Use water: 32
           Use food:27
           Health: 65
           Name: name
           Status: n

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
  • at MainActivity, putParcelable methon doesn't exist. There's just putparcelableArrayList – InhoLee May 17 '17 at 20:02
  • how do you create object `ch1`? are you send object from fragment? – Ferdous Ahamed May 17 '17 at 20:13
  • I make the object in MainFragment, Like this Character ch1 = new Character(27,32,65,"name","n"); and I use set method to initialize data. ch1.setNow_water(100); Like this. This one make error? – InhoLee May 17 '17 at 20:14
0

you need to move your code inside readFromParcel to public Character(Parcel in)

you can simply delete readFromParcel method, its useless.

updated constructor would be will be:

public Character(Parcel in){
        use_food = in.readInt();
        use_water = in.readInt();
        health = in.readInt();
        name = in.readString();
    } 
Saurabh
  • 1,225
  • 9
  • 16