带包裹的EXTRA输出为NULL,但没有问题 [英] EXTRA with parcelable comes out NULL but without its fine

查看:143
本文介绍了带包裹的EXTRA输出为NULL,但没有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于似乎无法完全理解的问题,我感到有些沮丧.

I am getting a bit frustrated with an issue that I cannot seem to fully understand.

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

I have a listview with items and when I click them I want to pass an object (Parcelable) to a new activity. This is the code below:

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

现在,我还有多余的"i"用于调试.我只是发送"entry",当我对活动不感兴趣时​​.下面的代码:

Now, I have the "i" extra there for debugging purposes. I was just sending "entry" and when I got the intent on the activity it didn't work. Code below:

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"键(我已调试使用手表功能来读取i)

So when I read my bundle there is nothing at all. No "entry" key and no "i" key (I debugged to read i using watch feature)

但是!如果我不发送"entry"而仅发送"i",并且我调试以捕获"i",那么我就知道了!

BUT! If I don't send "entry" and only send "i" and I debug to catch "i" I do get it!

我不知道为什么发送条目会破坏事情,但是我找不到任何答案.我调试了该对象,并确实通过.get(position)找到了它.

I have no idea why sending entry is ruining things but I cannot find any answer. I debugged the object and it does find it though .get(position).

希望任何人都可以给我任何想法,对于任何麻烦,我们深表歉意.

Hope anyone can give me any ideas, and sorry for any trouble.

编辑

下面是输入代码:

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

}

痒类也是可拆分的.我使用它正确填充了ListView(至少在Android上没有崩溃).

Itch class is also Parceable. I am populating correctly (no crashes on Android at least) the ListView with it.

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

For convenience I place the code here aswell:

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

}

推荐答案

好的,...是什么问题?很简单.

Alright so... What was the problem? Simple.

可包裹物始终为空的原因是因为发生了愚蠢的错误.哪个错误?

The reason why the parcelable always came out null was because a stupid error was occurring. Which error?

好吧,好吧,看一下这段代码:

Well, okay so take a look at this piece of code:

entry = b.getParcelable("entry");

这是什么意思?据说输入将等于可打包的"entry"键.但这到底是什么意思?看一下条目构造器.

What is it saying? It is saying that entry will be equal to the parcelable "entry" key. But what does that really mean? Look at entry constructor.

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

因此,当您说入口等于一个可包裹土地时,您将在我发布的Entry类中调用此构造函数.但是为什么您可能会问错呢?

So when you say that entry is equals to a parcelable, then you will call this constructor in the Entry class that I have posted. But why is it wrong you might ask?

好吧,所以看看.我们给ArrayList发痒给方法readTypeList.但是...等等如果那是一个构造函数,则意味着我们要从0开始构建...那么...是否开始发痒?不它不是!因为我只是在常规"构造函数中开始发痒!

Well, so take a look. We're giving ArrayList itches to the method readTypeList. but... wait a second. If that is a constructor that means that we're building from 0... So... is itches initiated? No it is not! Because I was only initiating itches in the "normal" constructor!

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

所以解决方案是...

So the solution is...

// PARCELABLE
public Entry(Parcel source){
    date = source.readString();

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

    source.readTypedList(itches, Itch.CREATOR);
}

就这样.那解决了我们的问题! :)

And thats it. That fixes our problem! :)

如果发生其他错误,请注意: 确保您的密钥正确.因此,请检查是否有其他错别字.

If other error occurs please be aware: Make SURE that your key is correct. So check out for any typos in your getting extras.

entry = b.getParcelable("entyr");

原样代替

entry = b.getParcelable("entry");

以及其他任何类型的错误.

And any other type of error like that.

这不是一个好主意,您应该有一个上面写有入口"的变量,这样您就永远不会出现这种类型的错误.我在我的代码中有它,因为我正在快速编程以构建原型:)

That is not a good practive, you should have a variable that has the "entry" written on it so you never have this type of error mistakes. I have it in my code because I am fast-programming to build up a prototype :)

祝您编程愉快!

这篇关于带包裹的EXTRA输出为NULL,但没有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆