Java 如何使用Android Parcelable移动数据?

Java 如何使用Android Parcelable移动数据?,java,android,parcelable,Java,Android,Parcelable,我正在做一个游戏,我需要移动一个自定义对象。为此,我使用一个包裹来移动数据 但是,我在移动数据时遇到问题,我不知道为什么 这是包裹类: 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

我正在做一个游戏,我需要移动一个自定义对象。为此,我使用一个包裹来移动数据

但是,我在移动数据时遇到问题,我不知道为什么

这是包裹类:

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;
    }
}
我使用按钮的单击事件移动此对象

最后,这是想要获取对象的活动

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));

为什么我的代码不起作用,我如何修复它?

按如下方式更新您的
Character
类:

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;
    }

}
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);
        }

    });
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);
D/Success: Use water: 32
           Use food:27
           Health: 65
           Name: name
           Status: n
Character1\u活动中检索
Character
对象。java

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;
    }

}
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);
        }

    });
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);
D/Success: Use water: 32
           Use food:27
           Health: 65
           Name: name
           Status: n
输出:

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;
    }

}
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);
        }

    });
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);
D/Success: Use water: 32
           Use food:27
           Health: 65
           Name: name
           Status: n

希望这会有所帮助~

您需要将您的代码从
读取包裹
移动到
公共字符(包裹中)

您可以简单地删除
readFromParcel
方法,它是无用的

更新的构造函数将是:

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

请阅读。带Parcel参数的构造函数是否应该为空?还有什么不起作用?值得一读:您是否将代码移动到指定的构造函数?请尝试。应解决所有问题。在MainActivity,不存在可编程方法。只有PutparcelableArrayList如何创建对象
ch1
?您是从fragment发送对象吗?我在MainFragment中创建对象,就像这个字符ch1=新字符(27,32,65,“name”,“n”);我使用set方法初始化数据。ch1.setNow_水(100);这样地。这个会出错吗?