在活动之间传递可序列化对象的问题

时间:2016-07-01 00:04:41

标签: java android android-intent serialization

我正在尝试将对象传递给某个活动。我知道有一百万个问题就像这个问题一样,我确信这里的某个地方是一个答案,但我似乎无法找到它而我无法让它发挥作用。我一直收到错误消息(起初我不断收到In [1155]: np.save('test.npy',np.array([1,2])) In [1156]: ls -l test.npy -rw-rw-r-- 1 paul paul 88 Jun 30 17:08 test.npy In [1157]: np.save('test.npy',np.arange(1000)) In [1158]: ls -l test.npy -rw-rw-r-- 1 paul paul 4080 Jun 30 17:08 test.npy ,而不是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);
}

我的Controller类实现了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)); }

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,但这似乎没有帮助;(

感谢阅读!

1 个答案:

答案 0 :(得分:2)

已经回答过了。 Passing JSONObject into another activity

“您可以简单地将整个JSONObject作为字符串。这样的事情:

i.putString(“product”,jsonObj.toString);

然后在MovieProductActivity中你可以

JSONObject jsonObj = new JSONObject(getIntent()。getStringExtra(“product”)); “