【问题标题】:Passing Serializable from one Activity to another does not work将 Serializable 从一个 Activity 传递到另一个 Activity 不起作用
【发布时间】: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


【解决方案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");
}

【讨论】:

  • 感谢您的回答,但事实证明 extras 始终为空。我明天用 parcelable 试试..
  • 抱歉,我在您的代码中遗漏了另一个问题(您是否忘记初始化 mBundle 对象?)。您可以尝试更新的答案。这样你就不需要自己显式初始化bundle了。
  • 你的意思是getIntent().getExtras();而不仅仅是意图?它仍然不起作用,extras ist null。
猜你喜欢
  • 2017-04-05
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
  • 2017-12-28
相关资源
最近更新 更多