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

标签 java android android-intent serialization

我想将一个对象从一个 Activity 传递到另一个 Activity 。只发送字符串和数组会很麻烦,因为我的对象包含一个带有其他一些对象的 vector 。我现在正在尝试阅读几个小时,但我就是无法让它发挥作用。

// 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 接口(interface)。

但是,这并不是导致您出现 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");
}

关于java - 将 Serializable 从一个 Activity 传递到另一个 Activity 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34144292/

相关文章:

java - SWTerror : Not implemented [multiple displays] in Eclipse plug-in devlopment 错误

android - 如何在 Kotlin 中创建通用适配器?

java - 尽管已设置标志 - 从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志

android - 如何通过 Intent 发送任意字符串?

java - 为什么我的 getY() 返回 0?

java - show() 时弹出菜单崩溃;

java - RecyclerView onClickItem 没有响应点击

android - 编辑文本和搜索按钮缩小并越界

android - 为什么我的听众没有在这个 Firestore 集中点击这里

java - 如何比较来自 Intent.getStringExtra() 的字符串?