【发布时间】:2015-12-07 22:14:19
【问题描述】:
我想将一个对象从一个 Activity 传递给另一个。只发送字符串和数组会很麻烦,因为我的对象包含一个带有其他对象的向量。我现在正在尝试和阅读几个小时,但我无法让它发挥作用。
// Room.java the object I want to send
public class Room implements Serializable {
private int heating;
private boolean light;
private String id;
private String name;
// A list of additional devices, may be empty
private Vector<Device> devices = new Vector();
...
}
// HomeScreen.java - here I call my intent to switch to RoomScreen
public void onOpenRoom(View view) {
Intent myIntent = new Intent(this, RoomScreen.class);
Room testRoom = new Room("1", "test", 1, true);
mBundle.putSerializable("roomObject", testRoom);
myIntent.putExtras(mBundle);
startActivity(myIntent);
}
// RoomScreen.java This is the activity where I want to use the passed object
public class RoomScreen extends AppCompatActivity {
private Room room;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room);
room = (Room) getIntent().getSerializableExtra("roomObject");
getSupportActionBar().setTitle(room.getName());
createElements();
}
但是我总是得到错误
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String automaton.automaton.Room.getName()' on a null object reference
我也尝试过使用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room);
Bundle myBundle = getIntent().getBundleExtra("roomObject");
room = (Room) myBundle.getSerializable("roomObject");
getSupportActionBar().setTitle(room.getName());
createElements();
}
然后我得到
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)' on a null object reference
我做错了什么?
Room.java 的完整代码
public class Room implements Serializable {
private static final long serialVersionUID = 7526472295622776147L;
private int heating;
private boolean light;
private String id;
private String name;
// A list of additional devices, may be empty
private Vector<Device> devices = new Vector();
public Room() {
}
/**
* Sets initial heating and light
*
* @param heating
* @param light
*/
public Room(String id, String name, int heating, boolean light) {
this.id = id;
this.heating = heating;
this.light = light;
this.name = name;
}
public boolean getLight() {
return this.light;
}
public int getHeating() {
return this.heating;
}
public String getName() {
return this.name;
}
/**
* Adds a new device to the room.
*
* @param device
*/
public void addDevice(Device device) {
this.devices.add(device);
}
/**
* Searches and returns a device by its id
*
* @param id
* @return Device
* @throws NoSuchElementException
*/
public Device getDeviceById(String id) throws NoSuchElementException {
boolean found = false;
int foundAt = 0;
for(int i = 0; i < devices.size(); i++) {
if(devices.elementAt(i).getId() == id) {
found = true;
foundAt = i;
break;
}
}
if(found) return devices.elementAt(foundAt);
else throw new NoSuchElementException("Device not found!");
}
/**
* Returns all devices attached to the room
*/
public Vector<Device> getDevices() {
return this.devices;
}
/**
* Returns the id of the room.
*
* @return
*/
public String getId() {
return this.id;
}
}
【问题讨论】:
-
首先链接实现可序列化的对象的整个代码,这是我们会发现你的错误的地方
-
是的,我们看不到对象在哪里被实例化。
-
您可能还想考虑使用 Parcelable 而不是 Serializable。
-
使用 Parcelable 可能更容易,因为它们更适合活动之间的数据传输 -> 活动
-
我添加了 Room.java 的完整代码。现在尝试用 parcelable 解决它!
标签: java android android-intent serialization