将Serializable从一个Activity传递到另一个Activity不起作用

时间:2015-12-07 22:14:20

标签: java android android-intent serialization

我想将一个Object从一个Activity传递给另一个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;
}
}

1 个答案:

答案 0 :(得分:1)

我还建议您考虑使用更优化的Parcelable接口。

但是,这不是导致你NPE的问题。您尝试附加和检索Room对象的方式不正确。

更新:

附上您的可序列化对象:

Intent myIntent = new Intent(this, RoomScreen.class);
Room testRoom = new Room("1", "test", 1, true);
myIntent.putExtra("roomObject", testRoom);

然后,像这样得到你的对象:

Bundle bundle = intent.getExtras();
if (bundle != null) {
    room = (Room) bundle.getSerializable("roomObject");
}