Android:可包裹 returns 无
Android: Parcelable returns nothing
我遇到以下问题:我想将一个对象从一个 activity 包裹到另一个。
是这个对象:
public class Item implements Parcelable {
private int minimumAmountOfItems;
private String nameOfItem;
private int maximumAmountOfItems;
private int actualAmountOfItems;
public Item(int minimumAmountOfItems, String nameOfItem, int maximumAmountOfItems, int actualAmountOfItems){
this.minimumAmountOfItems = minimumAmountOfItems;
this.nameOfItem = nameOfItem;
this.maximumAmountOfItems = maximumAmountOfItems;
this.actualAmountOfItems = actualAmountOfItems;
}
具有以下可打包编码内容:
@Override
public int describeContents() {
/*Necessary for the parcelable stuff; but not needed in about 99.9% of the cases (source: Stack Overflow)*/
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
/*Store the data in a parcelable object to communicate between Activities */
dest.writeInt(this.minimumAmountOfItems);
dest.writeString(this.nameOfItem);
dest.writeInt(this.maximumAmountOfItems);
dest.writeInt(this.actualAmountOfItems);
}
private Item(Parcel in){
/*Retrieve the data that was packaged before*/
this.minimumAmountOfItems = in.readInt();
this.nameOfItem = in.readString();
this.maximumAmountOfItems = in.readInt();
this.actualAmountOfItems = in.readInt();
}
public static final Parcelable.Creator<Item> CREATOR = new Parcelable.Creator<Item>(){
/*This small part of code passes along the unmarshalled parcel and returns the next object.*/
@Override
public Item createFromParcel(Parcel in){
return new Item(in);
}
@Override
public Item[] newArray(int size){
return new Item[size];
}
};
因此,我在我的活动中使用以下代码来打包我的对象:
resultIntent = new Intent(ItemCreatorActivity.this, ItemListEditorActivity.class);
resultIntent.putExtra("Item", item);
startActivity(resultIntent);
为了得到我想要实现的一切:
Intent inputIntent = getIntent();
Item passedItem = inputIntent.getExtras().getParcelable("Item");
System.out.println(passedItem.getNameOfItem());
现在我有几个问题:
1.) 我如何检查从发件人到收件人的可包裹信息是否有效?
我认为我已经从本教程中获得了关于可打包对象的所有重要信息:https://coderwall.com/p/vfbing/passing-objects-between-activities-in-android 其他几个(出于可读性目的,我不会在这里 post 它们,因为它们都描述了相同的过程和结构.. .);但我的 System.out.println(passedItem.getNameOfItem()) 仍然没有抛出任何东西。我的 AndroidMonitor 中没有更多消息。
2.)我曾调用此 activity 一次,当时我还没有创建 Item 对象。是否有更多 "smooth" 方法来防止此代码中第一次调用 activity 时出现空指针异常?
3.) 当我按下一个按钮,切换到一个新的 Activity,创建一个 Item 对象并想象一下,我的代码以前可以工作,我可以将我的对象传递回调用 Activity。我之前通过调用 Activity 保存在 ArrayList 中的所有对象是否仍然可用,还是我必须将我的 ArrayList 传递给新的 Activity 然后返回到调用 Activity?
提前感谢您的帮助。
此致
您正在 Intent 中添加可打包对象并从 Bundle 中检索它(inputIntent.getExtras() returns Bundle 类型的对象)。
所以需要改成inputIntent.getParcelableExtra("Item");
3.) 当我按下一个按钮,切换到一个新的 Activity,创建一个 Item 对象并想象一下,我的代码以前可以工作,我可以将我的对象传递回调用 Activity。我之前通过调用 Activity 保存在 ArrayList 中的所有对象是否仍然可用,还是我必须将我的 ArrayList 传递给新的 Activity 然后返回到调用 Activity?
是的。您可以将 ArrayList 添加到意图对象:
ArrayList<Item> itemList = new ArrayList<Item>();
// Add elements to the list
intent.putParcelableArrayListExtra("Item_List", itemList);
并检索它:
ArrayList<Item> itemList = inputIntent.getParcelableArrayListExtra("Item_List");
1.) How can I check, whether I get valid parcelable information from my sender to my receiver? I think that I have got everything important about parcelable objects from this tutorial: https://coderwall.com/p/vfbing/passing-objects-between-activities-in-android several others (due to readability purposes I won't post them here, because they all describe the same procedure and structure...); but still my System.out.println(passedItem.getNameOfItem()) throws nothing. And there are no further messages in my AndroidMonitor.
您可以通过
登录到您的 logcat
Log.d("tag", passedItem.getNameOfItem());
并查看您的 logcat。它应该打印出你的数据
2.)I am calling this activity one time, when I do not have created an Item object. Is there a more "smooth" way to prevent a nullpointerexception in this code for the very first call of the activity?
您可以在收货时这样做 activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
Item passedItem = (Item) extras.getParcelable("Item");
}
3.) When I am pressing on a button, switching to a new Activity, creating an Item object and imagine, my code would have worked before and I could pass my object back to the calling Activity. Will all objects, that I have saved before in an ArrayList from the calling Activity, still be available or do I have to pass my ArrayList to the new Activity and then back to the calling Activity?
至于这个,我会研究 http://developer.android.com/training/basics/activity-lifecycle/recreating.html,它将教您如何通过状态重新创建活动和保存数据。
我遇到以下问题:我想将一个对象从一个 activity 包裹到另一个。
是这个对象:
public class Item implements Parcelable {
private int minimumAmountOfItems;
private String nameOfItem;
private int maximumAmountOfItems;
private int actualAmountOfItems;
public Item(int minimumAmountOfItems, String nameOfItem, int maximumAmountOfItems, int actualAmountOfItems){
this.minimumAmountOfItems = minimumAmountOfItems;
this.nameOfItem = nameOfItem;
this.maximumAmountOfItems = maximumAmountOfItems;
this.actualAmountOfItems = actualAmountOfItems;
}
具有以下可打包编码内容:
@Override
public int describeContents() {
/*Necessary for the parcelable stuff; but not needed in about 99.9% of the cases (source: Stack Overflow)*/
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
/*Store the data in a parcelable object to communicate between Activities */
dest.writeInt(this.minimumAmountOfItems);
dest.writeString(this.nameOfItem);
dest.writeInt(this.maximumAmountOfItems);
dest.writeInt(this.actualAmountOfItems);
}
private Item(Parcel in){
/*Retrieve the data that was packaged before*/
this.minimumAmountOfItems = in.readInt();
this.nameOfItem = in.readString();
this.maximumAmountOfItems = in.readInt();
this.actualAmountOfItems = in.readInt();
}
public static final Parcelable.Creator<Item> CREATOR = new Parcelable.Creator<Item>(){
/*This small part of code passes along the unmarshalled parcel and returns the next object.*/
@Override
public Item createFromParcel(Parcel in){
return new Item(in);
}
@Override
public Item[] newArray(int size){
return new Item[size];
}
};
因此,我在我的活动中使用以下代码来打包我的对象:
resultIntent = new Intent(ItemCreatorActivity.this, ItemListEditorActivity.class);
resultIntent.putExtra("Item", item);
startActivity(resultIntent);
为了得到我想要实现的一切:
Intent inputIntent = getIntent();
Item passedItem = inputIntent.getExtras().getParcelable("Item");
System.out.println(passedItem.getNameOfItem());
现在我有几个问题:
1.) 我如何检查从发件人到收件人的可包裹信息是否有效? 我认为我已经从本教程中获得了关于可打包对象的所有重要信息:https://coderwall.com/p/vfbing/passing-objects-between-activities-in-android 其他几个(出于可读性目的,我不会在这里 post 它们,因为它们都描述了相同的过程和结构.. .);但我的 System.out.println(passedItem.getNameOfItem()) 仍然没有抛出任何东西。我的 AndroidMonitor 中没有更多消息。
2.)我曾调用此 activity 一次,当时我还没有创建 Item 对象。是否有更多 "smooth" 方法来防止此代码中第一次调用 activity 时出现空指针异常?
3.) 当我按下一个按钮,切换到一个新的 Activity,创建一个 Item 对象并想象一下,我的代码以前可以工作,我可以将我的对象传递回调用 Activity。我之前通过调用 Activity 保存在 ArrayList 中的所有对象是否仍然可用,还是我必须将我的 ArrayList 传递给新的 Activity 然后返回到调用 Activity?
提前感谢您的帮助。
此致
您正在 Intent 中添加可打包对象并从 Bundle 中检索它(inputIntent.getExtras() returns Bundle 类型的对象)。
所以需要改成inputIntent.getParcelableExtra("Item");
3.) 当我按下一个按钮,切换到一个新的 Activity,创建一个 Item 对象并想象一下,我的代码以前可以工作,我可以将我的对象传递回调用 Activity。我之前通过调用 Activity 保存在 ArrayList 中的所有对象是否仍然可用,还是我必须将我的 ArrayList 传递给新的 Activity 然后返回到调用 Activity?
是的。您可以将 ArrayList 添加到意图对象:
ArrayList<Item> itemList = new ArrayList<Item>();
// Add elements to the list
intent.putParcelableArrayListExtra("Item_List", itemList);
并检索它:
ArrayList<Item> itemList = inputIntent.getParcelableArrayListExtra("Item_List");
1.) How can I check, whether I get valid parcelable information from my sender to my receiver? I think that I have got everything important about parcelable objects from this tutorial: https://coderwall.com/p/vfbing/passing-objects-between-activities-in-android several others (due to readability purposes I won't post them here, because they all describe the same procedure and structure...); but still my System.out.println(passedItem.getNameOfItem()) throws nothing. And there are no further messages in my AndroidMonitor.
您可以通过
登录到您的 logcatLog.d("tag", passedItem.getNameOfItem());
并查看您的 logcat。它应该打印出你的数据
2.)I am calling this activity one time, when I do not have created an Item object. Is there a more "smooth" way to prevent a nullpointerexception in this code for the very first call of the activity?
您可以在收货时这样做 activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
Item passedItem = (Item) extras.getParcelable("Item");
}
3.) When I am pressing on a button, switching to a new Activity, creating an Item object and imagine, my code would have worked before and I could pass my object back to the calling Activity. Will all objects, that I have saved before in an ArrayList from the calling Activity, still be available or do I have to pass my ArrayList to the new Activity and then back to the calling Activity?
至于这个,我会研究 http://developer.android.com/training/basics/activity-lifecycle/recreating.html,它将教您如何通过状态重新创建活动和保存数据。