可分配的数据返回null

时间:2011-03-17 21:08:02

标签: android

我有以下三个班级。

板球

 public class Cricketers implements Parcelable {

        private String name;
        private String address;
        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }


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

        public Cricketers() {
        }

        @Override
        public void writeToParcel(Parcel dest, int flag) {
            dest.writeString(name);
            dest.writeString(address);
            dest.writeInt(age);
        }

        public Cricketers(Parcel in) {
            this.name = in.readString();
            this.address = in.readString();
            this.age = in.readInt();
        }

        @SuppressWarnings("unchecked")
        public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
            public Cricketers createFromParcel(Parcel in) {
                return new Cricketers(in);
            }

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

活性1

public class Activity1 extends Activity {

    private ArrayList<Cricketers> cricketers;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        cricketers = new ArrayList<Cricketers>();

        Cricketers object = new Cricketers();
        object.setName("Sachin");
        object.setAddress("Mumbai");
        object.setAge(37);
        cricketers.add(object);

        Cricketers object1 = new Cricketers();
        object1.setName("Sourav");
        object1.setAddress("Kolkata");
        object1.setAge(38);
        cricketers.add(object1);

        Cricketers object2 = new Cricketers();
        object2.setName("Dravid");
        object2.setAddress("Bengaluru");
        object2.setAge(38);
        cricketers.add(object2);

        Intent intent = new Intent(this, Activity2.class);
        intent.putExtra("cricketers", cricketers);
        startActivity(intent);

        cricketers = null;

    }

活性2

public class Activity2 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        Cricketers cricketers = bundle.getParcelable("cricketers");

    }

}

我正在检索Activity2上的数据,但是它返回的cricketers = null。

在Activity1类中,数据“cricketers”正在被正确添加到Intent中。

请提供您的意见。

提前致谢。

2 个答案:

答案 0 :(得分:0)

我已经解决了这个问题......

需要对 Activity2 类进行更改

public class Activity2 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        ArrayList<Cricketers> data = new ArrayList<Cricketers>();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        data = bundle.getParcelableArrayList("cricketers");

        for (int count = 0; count < data.size(); count++) {

            Cricketers cric = (Cricketers) data.get(count);
            String name = cric.getName();
            String address = cric.getAddress();
            int age = cric.getAge();
        }
    }
}

答案 1 :(得分:0)

我发现AIDL缓存存在一些问题...如果您将所有代码更改为parcelable,或者简单,则重构该类的名称。如果您重新安装应用程序,或清除应用程序缓存,AIDL仍在工作并搜索重构的类。

我修复此任务重启设备。它是一个废话来改变parcelable类中的代码,但在重新安装应用程序时看不到它,你必须重启设备......或者那就是我做的。

感谢示例dude;)activit2是一个祝福!!