我正在尝试将一个对象传递给一个 Activity 。我知道有一百万个问题就像这个问题一样,而且我确信这里的某个地方有答案,但我似乎找不到它而且我无法让它发挥作用。我一直收到错误消息(起初我一直收到 NullPointerException
,不是 java.lang.RuntimeException: Parcelable encountered IOException writing serializable object
)。
这是我试图从中传递它的方法:
public void createView(){
Intent mIntent = new Intent(getMain(), ButtonLayout.class);
mIntent.putExtra("controller", getController());
getMain().startActivity(mIntent);
}
到这里:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.button_panel);
this.controller = (Controller) getIntent().getSerializableExtra("controller");
calculate = (Button) findViewById(R.id.calc);
calculate.setOnClickListener(new WorkAppOnClickListener(this.controller));
}
我的 Controller 类实现了Serializable
:
import java.io.Serializable;
public class Controller implements Serializable{
private Calculate currentActivity;
private ButtonLayoutModel buttonLayout;
AvModel av;
public void setCurrentActivity(Calculate active){
this.currentActivity = active;
}
public Calculate getCurrentActivity(){
return this.currentActivity;
}
public void setAv(AvModel av){
this.av = av;
}
public AvModel getAv(){
return this.av;
}
public void setButtonLayout(ButtonLayoutModel bl){
this.buttonLayout = bl;
}
public ButtonLayoutModel getButtonLayout(){
return this.buttonLayout;
}
}
我尝试转换为 Serializable
而不是 Controller,我尝试转换为 getSerializableExtra
,但这似乎也无济于事;(
感谢阅读!
最佳答案
已在 : Passing JSONObject into another activity 上得到答复
您可以简单地将整个 JSONObject 作为一个字符串。像这样:
i.putString("product", jsonObj.toString);
然后在 MovieProductActivity 中你可以
JSONObject jsonObj = new JSONObject(getIntent().getStringExtra("product"));
关于java - 在 Activity 之间传递可序列化对象的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38135093/