Android 带包裹的额外费用为空,但无罚款

Android 带包裹的额外费用为空,但无罚款,android,listview,android-intent,null,parcelable,Android,Listview,Android Intent,Null,Parcelable,我对一个我似乎无法完全理解的问题感到有点沮丧 我有一个包含项目的列表视图,当我单击它们时,我想将一个对象(可包裹)传递给一个新的活动。代码如下: lv_Entries.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

我对一个我似乎无法完全理解的问题感到有点沮丧

我有一个包含项目的列表视图,当我单击它们时,我想将一个对象(可包裹)传递给一个新的活动。代码如下:

lv_Entries.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent getItchesScreen = new Intent(Home.this, Itches.class);

            getItchesScreen.putExtra("i", 3);

            Entry e = entries.get(position);

            getItchesScreen.putExtra("entry", e);

            startActivity(getItchesScreen);
        }
    });
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_itches);

    tv_date = (TextView) findViewById(R.id.tv_date);

    Bundle b = getIntent().getExtras();

    entry = b.getParcelable("entry");

    tv_date.setText(entry.getDate());

    itches = entry.getItches();

    itchesAdapter = new ItchAdapter(this, itches);

    ListView lv_Itches = (ListView) findViewById(R.id.lv_itches);

    lv_Itches.setAdapter(itchesAdapter);
}
所以当我读我的包时,什么都没有。没有“entry”键和“i”键(我调试为使用watch功能读取i)

但是!如果我不发送“entry”而只发送“I”,并且我调试以捕获“I”,那么我会得到它

我不知道为什么发送条目会毁掉一切,但我找不到任何答案。我调试了对象,但它确实找到了它。get(position)

希望任何人能给我任何想法,并为任何麻烦道歉

编辑

以下是输入代码:

public class Entry implements Parcelable{

private String date;
private ArrayList<Itch> itches;

public Entry(String date){
    this.date = date;
    itches = new ArrayList<Itch>();
}

// PARCELABLE
public Entry(Parcel source){
    date = source.readString();
    source.readTypedList(itches, Itch.CREATOR);
}

public void AddItch(Itch itch){
    itches.add(itch);
}

// get intensity average for the itches
public int IntensityAverage(){

    int intensity = 0;

    for(Itch i : itches){
        intensity += i.getIntensity();
    }

    return intensity/itches.size();
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public ArrayList<Itch> getItches() {
    return itches;
}

public void setItches(ArrayList<Itch> itches) {
    this.itches = itches;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(date);
    dest.writeTypedList(itches);
}

public static final Parcelable.Creator<Entry> CREATOR =
    new Parcelable.Creator<Entry>() {
        public Entry createFromParcel(Parcel source) {
            return new Entry(source);
        }

        public Entry[] newArray(int size) {
            return new Entry[size];
        }
    };
公共类条目实现可包裹{
私有字符串日期;
私人ArrayList瘙痒;
公共条目(字符串日期){
this.date=日期;
itches=newarraylist();
}
//包裹的
公共入口(包裹来源){
日期=source.readString();
source.readTypedList(itches,Itch.CREATOR);
}
公共空间附加信息(瘙痒){
痒。添加(痒);
}
//获得瘙痒的平均强度
公共照明强度平均值(){
int强度=0;
用于(瘙痒i:瘙痒){
强度+=i.getIntensity();
}
返回强度/瘙痒。大小();
}
公共字符串getDate(){
返回日期;
}
公共无效设置日期(字符串日期){
this.date=日期;
}
公共数组列表getItches(){
返痒;
}
公共无效设置(ArrayList设置){
这个。痒=痒;
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的地书面资本(日期);
目的地writeTypedList(itches);
}
公共静态最终包裹。创建者=
新建Parcelable.Creator(){
公共条目createFromParcel(地块源){
返回新条目(来源);
}
公共条目[]新数组(整数大小){
返回新条目[大小];
}
};
}

瘙痒类也是可分配的。我用它正确地填充了ListView(至少在Android上没有崩溃)

为了方便起见,我也将代码放在这里:

public class Itch implements Parcelable{

private String time;
private String local;
private int intensity;

public Itch(String time, String local, int intensity){
    this.time = time;
    this.local = local;
    this.intensity = intensity;
}

// PARCELABLE
public Itch(Parcel source){
    time = source.readString();
    local = source.readString();
    intensity = source.readInt();
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

public String getLocal() {
    return local;
}

public void setLocal(String local) {
    this.local = local;
}

public int getIntensity() {
    return intensity;
}

public void setIntensity(int intensity) {
    this.intensity = intensity;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(time);
    dest.writeString(local);
    dest.writeInt(intensity);
}

public static final Parcelable.Creator<Itch> CREATOR =
    new Parcelable.Creator<Itch>() {
        public Itch createFromParcel(Parcel source) {
            return new Itch(source);
        }

        public Itch[] newArray(int size) {
            return new Itch[size];
        }
    };
公共类Itch实现可包裹{
私有字符串时间;
私有字符串本地;
私人智力强度;
公共瘙痒(字符串时间、字符串局部、整数强度){
这个时间=时间;
this.local=本地;
强度=强度;
}
//包裹的
公众瘙痒(包裹来源){
时间=source.readString();
local=source.readString();
强度=source.readInt();
}
公共字符串getTime(){
返回时间;
}
公共无效设置时间(字符串时间){
这个时间=时间;
}
公共字符串getLocal(){
返回本地;
}
公共void setLocal(字符串本地){
this.local=本地;
}
public int getIntensity(){
回归强度;
}
公共空间强度(内部强度){
强度=强度;
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的地书面限制(时间);
目的地书面限制(本地);
目的写入(强度);
}
公共静态最终包裹。创建者=
新建Parcelable.Creator(){
public Itch createFromParcel(地块源){
返回新瘙痒(来源);
}
public Itch[]新数组(整数大小){
返回新的瘙痒[大小];
}
};

}

您是否尝试过在
onCreate()中执行此操作


好吧,那么。。。出了什么问题?简单

包裹总是显示为空的原因是发生了一个愚蠢的错误。哪个错误

好的,那么看看这段代码:

entry = b.getParcelable("entry");
它在说什么?这意味着入口将等于可包裹的“入口”键。但这到底意味着什么?看看条目构造函数

// PARCELABLE
public Entry(Parcel source){
    date = source.readString();
    source.readTypedList(itches, Itch.CREATOR);
}
所以当你说entry等于一个parcelable时,你将在我发布的entry类中调用这个构造函数。但你可能会问,为什么这是错误的

好吧,那就看一看。我们正在对readTypeList方法进行ArrayList测试。但是等一下。如果这是一个构造函数,这意味着我们正在从0构建。。。所以瘙痒是由什么引起的?不,不是!因为我只是在“正常”构造器中引发瘙痒

public Entry(String date){
    this.date = date;
    itches = new ArrayList<Itch>();
}
代替

entry = b.getParcelable("entry");
还有其他类似的错误

这不是一个好的实践,你应该有一个变量,上面写着“条目”,这样你就不会有这种类型的错误。我的代码中有它,因为我正在快速编程以构建原型:)


快乐编码

您从未在那里调用b.getInt(“i”)(您必须在不测试时将其取出)。如果条目为空或包装不当,您将遇到问题。我如何知道它是否包装不当?编辑:是的,我没有“我”在那里。我通常通过调试“watch”来做这种测试。你写的是入门包还是Android原生入门类?你能发布它吗?这样我们就可以看看你是否可能在打包时犯了错误。好的,我发现了问题,我会在我的文章的最后写出来。谢谢你的帮助:)这对我有什么帮助?因为这只说明,如果我没有任何键“条目”,除了记录它,我什么都不做。我总是有一个条目。希望我的话听起来没那么刺耳,抱歉。
// PARCELABLE
public Entry(Parcel source){
    date = source.readString();

    //add this if condition!
    if (itches == null) {
        itches = new ArrayList<Itch>();
    }

    source.readTypedList(itches, Itch.CREATOR);
}
entry = b.getParcelable("entyr");
entry = b.getParcelable("entry");