为什么我的 parceled 对象返回为空? Android 开发
Why is my parceled object coming back as null? Android Dev
我一直在尝试打包一个对象,return它对 MainActivity 方法很有价值。当我在我的 AddItemActivity 中记录 mItem 对象时,所有对象的属性都已分配,然后再将其全部签出。我的数据在某个地方丢失了......无论是从我创建新 Intent 的一开始,还是在 MainActivity 的接收端。我想做的是……获取用户输入,创建项目对象,将该对象传递给 MainActivity,然后将其添加到 Inventory。有什么想法吗?
这是 MainActivity:
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.gardnerwebideas.inventorytracker.R;
import com.gardnerwebideas.inventorytracker.adapters.ItemAdapter;
import com.gardnerwebideas.inventorytracker.models.Inventory;
import com.gardnerwebideas.inventorytracker.models.Item;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends ListActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private Item mNewItem;
private Inventory mInventory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addButton = (Button) findViewById(R.id.addToInventoryButton);
mInventory = new Inventory();
mNewItem = new Item();
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addToInventory();
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (1): {
if (resultCode == Activity.RESULT_OK) {
Intent intent = getIntent();
mNewItem = intent.getParcelableExtra(AddItemActivity.ADD_ITEM);
new ItemAdapter(this, mInventory);
Toast.makeText(this,
"Inventory size: "
+ String.valueOf(mInventory.getSize())
+ mInventory.toString(),
Toast.LENGTH_LONG).show();
}
break;
}
default:
break;
}
}
public void addToInventory() {
Intent intent = new Intent(this, AddItemActivity.class);
startActivityForResult(intent, 1);
}
}
这是 AddItemActivity:
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.gardnerwebideas.inventorytracker.models.Item;
import com.gardnerwebideas.inventorytracker.R;
import java.util.Date;
public class AddItemActivity extends AppCompatActivity {
public static final String TAG = AddItemActivity.class.getSimpleName();
public static final String ADD_ITEM = "ADD_ITEM";
private Item mItem;
private String mName;
private String mDate;
private String mDescription;
private String mPrice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
Button addButton = (Button) findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createNewItem();
mItem = new Item(mName, mDescription, mPrice, mDate);
Log.d(TAG, "THE ITEM IS: " + mItem.toString());
Intent intent = new Intent();
intent.putExtra(ADD_ITEM, mItem);
setResult(Activity.RESULT_OK, intent);
finish();
}
});
}
private void createNewItem() {
EditText itemName = (EditText) findViewById(R.id.nameField);
EditText itemDescription = (EditText) findViewById(R.id.descriptionField);
EditText itemPrice = (EditText) findViewById(R.id.priceField);
mName = itemName.getText().toString();
mDescription = itemDescription.getText().toString();
mPrice = itemPrice.getText().toString();
mDate = String.valueOf(new Date());
}
}
项目class:
import android.os.Parcel;
import android.os.Parcelable;
import java.util.Date;
public class Item implements Parcelable {
private String mName;
private String mInventoryDate;
private String mDescription;
private String mPrice;
public Item(String name, String description, String price, String stringDate) {
mName = name;
mInventoryDate = stringDate;
mDescription = description;
mPrice = price;
}
public String[] getItemInfo() {
String[] itemInfo = {mName, mDescription, mInventoryDate, mPrice};
return itemInfo;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getInventoryDate() {
return mInventoryDate;
}
public void setInventoryDate(String stringDate) {
mInventoryDate = stringDate;
}
public String getDescription() {
return mDescription;
}
public void setDescription(String description) {
mDescription = description;
}
public String getPrice() {
return mPrice;
}
public void setPrice(String price) {
mPrice = price;
}
@Override
public String toString() {
return mName + " " + mInventoryDate + " " + mDescription + " " + mPrice;
}
@Override
public int describeContents() {
return 0; //Not necessary...
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mName);
dest.writeString(mInventoryDate);
dest.writeString(mDescription);
dest.writeString(mPrice);
}
private Item(Parcel in) {
mName = in.readString();
mInventoryDate = in.readString();
mDescription = in.readString();
mPrice = in.readString();
}
public static final Creator<Item> CREATOR = new Creator<Item>() {
@Override
public Item createFromParcel(Parcel source) {
return new Item(source);
}
@Override
public Item[] newArray(int size) {
return new Item[size];
}
};
}
和库存class:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Inventory {
private List<Item> mInventoryItems;
public Inventory() {
mInventoryItems = new ArrayList<>();
}
public int getSize() {
return mInventoryItems.size();
}
public void addItem(Item item) {
mInventoryItems.add(item);
}
public Item getPosition(int position) {
return mInventoryItems.get(position);
}
public List<Item> getInventoryItems() {
return mInventoryItems;
}
public void setInventoryItems(List<Item> items) {
mInventoryItems = items;
}
@Override
public String toString() {
return "Inventory{" +
"mInventoryItems=" + mInventoryItems +
'}';
}
}
我已经花了大约 18 个小时试图弄清楚这个问题,但我只是遗漏了一些最后的部分,我只知道。
问题出在您的 onActivityResult()
方法中,您在其中调用:
Intent intent = getIntent();
getIntent()
returns 用于创建 Activity 的 Intent。您想使用传递给 onActivityResult()
方法的 Intent,所以:
mNewItem = data.getParcelableExtra(AddItemActivity.ADD_ITEM);
我一直在尝试打包一个对象,return它对 MainActivity 方法很有价值。当我在我的 AddItemActivity 中记录 mItem 对象时,所有对象的属性都已分配,然后再将其全部签出。我的数据在某个地方丢失了......无论是从我创建新 Intent 的一开始,还是在 MainActivity 的接收端。我想做的是……获取用户输入,创建项目对象,将该对象传递给 MainActivity,然后将其添加到 Inventory。有什么想法吗?
这是 MainActivity:
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.gardnerwebideas.inventorytracker.R;
import com.gardnerwebideas.inventorytracker.adapters.ItemAdapter;
import com.gardnerwebideas.inventorytracker.models.Inventory;
import com.gardnerwebideas.inventorytracker.models.Item;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends ListActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private Item mNewItem;
private Inventory mInventory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addButton = (Button) findViewById(R.id.addToInventoryButton);
mInventory = new Inventory();
mNewItem = new Item();
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addToInventory();
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (1): {
if (resultCode == Activity.RESULT_OK) {
Intent intent = getIntent();
mNewItem = intent.getParcelableExtra(AddItemActivity.ADD_ITEM);
new ItemAdapter(this, mInventory);
Toast.makeText(this,
"Inventory size: "
+ String.valueOf(mInventory.getSize())
+ mInventory.toString(),
Toast.LENGTH_LONG).show();
}
break;
}
default:
break;
}
}
public void addToInventory() {
Intent intent = new Intent(this, AddItemActivity.class);
startActivityForResult(intent, 1);
}
}
这是 AddItemActivity:
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.gardnerwebideas.inventorytracker.models.Item;
import com.gardnerwebideas.inventorytracker.R;
import java.util.Date;
public class AddItemActivity extends AppCompatActivity {
public static final String TAG = AddItemActivity.class.getSimpleName();
public static final String ADD_ITEM = "ADD_ITEM";
private Item mItem;
private String mName;
private String mDate;
private String mDescription;
private String mPrice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
Button addButton = (Button) findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createNewItem();
mItem = new Item(mName, mDescription, mPrice, mDate);
Log.d(TAG, "THE ITEM IS: " + mItem.toString());
Intent intent = new Intent();
intent.putExtra(ADD_ITEM, mItem);
setResult(Activity.RESULT_OK, intent);
finish();
}
});
}
private void createNewItem() {
EditText itemName = (EditText) findViewById(R.id.nameField);
EditText itemDescription = (EditText) findViewById(R.id.descriptionField);
EditText itemPrice = (EditText) findViewById(R.id.priceField);
mName = itemName.getText().toString();
mDescription = itemDescription.getText().toString();
mPrice = itemPrice.getText().toString();
mDate = String.valueOf(new Date());
}
}
项目class:
import android.os.Parcel;
import android.os.Parcelable;
import java.util.Date;
public class Item implements Parcelable {
private String mName;
private String mInventoryDate;
private String mDescription;
private String mPrice;
public Item(String name, String description, String price, String stringDate) {
mName = name;
mInventoryDate = stringDate;
mDescription = description;
mPrice = price;
}
public String[] getItemInfo() {
String[] itemInfo = {mName, mDescription, mInventoryDate, mPrice};
return itemInfo;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getInventoryDate() {
return mInventoryDate;
}
public void setInventoryDate(String stringDate) {
mInventoryDate = stringDate;
}
public String getDescription() {
return mDescription;
}
public void setDescription(String description) {
mDescription = description;
}
public String getPrice() {
return mPrice;
}
public void setPrice(String price) {
mPrice = price;
}
@Override
public String toString() {
return mName + " " + mInventoryDate + " " + mDescription + " " + mPrice;
}
@Override
public int describeContents() {
return 0; //Not necessary...
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mName);
dest.writeString(mInventoryDate);
dest.writeString(mDescription);
dest.writeString(mPrice);
}
private Item(Parcel in) {
mName = in.readString();
mInventoryDate = in.readString();
mDescription = in.readString();
mPrice = in.readString();
}
public static final Creator<Item> CREATOR = new Creator<Item>() {
@Override
public Item createFromParcel(Parcel source) {
return new Item(source);
}
@Override
public Item[] newArray(int size) {
return new Item[size];
}
};
}
和库存class:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Inventory {
private List<Item> mInventoryItems;
public Inventory() {
mInventoryItems = new ArrayList<>();
}
public int getSize() {
return mInventoryItems.size();
}
public void addItem(Item item) {
mInventoryItems.add(item);
}
public Item getPosition(int position) {
return mInventoryItems.get(position);
}
public List<Item> getInventoryItems() {
return mInventoryItems;
}
public void setInventoryItems(List<Item> items) {
mInventoryItems = items;
}
@Override
public String toString() {
return "Inventory{" +
"mInventoryItems=" + mInventoryItems +
'}';
}
}
我已经花了大约 18 个小时试图弄清楚这个问题,但我只是遗漏了一些最后的部分,我只知道。
问题出在您的 onActivityResult()
方法中,您在其中调用:
Intent intent = getIntent();
getIntent()
returns 用于创建 Activity 的 Intent。您想使用传递给 onActivityResult()
方法的 Intent,所以:
mNewItem = data.getParcelableExtra(AddItemActivity.ADD_ITEM);