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?
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~
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.