【问题标题】:Problems with passing serializable object between activities在活动之间传递可序列化对象的问题
【发布时间】:2016-07-01 00:04:40
【问题描述】:

我正在尝试将对象传递给活动。我知道有一百万个像这样的问题,我敢肯定这里的某个地方是一个答案,但我似乎找不到它,我就是无法让它发挥作用。我不断收到错误消息(起初我一直收到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,但这似乎也无济于事 ;(

感谢阅读!

【问题讨论】:

  • 您不能序列化 Activity 或其任何子类。你的Controller 类真的需要扩展AppCompatActivity吗?
  • 哦,好吧,这很有意义。实际上,我认为不需要 :) 非常感谢,我现在就试试。
  • 不幸的是,我仍然有同样的错误:/
  • 好吧,我猜你在Controller 中有一些非Serializable 成员,但我们需要查看类和堆栈跟踪以确定问题所在.
  • 是的,我编辑了我的帖子,它现在有完整的控制器类。

标签: java android android-intent serialization


【解决方案1】:

已经回答了:Passing JSONObject into another activity

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

i.putString("product", jsonObj.toString);

然后你可以在 MovieProductActivity 中

JSONObject jsonObj = new JSONObject(getIntent().getStringExtra("product"));

【讨论】:

  • 抱歉,我不太清楚你的意思?
猜你喜欢
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2013-01-29
  • 2015-09-17
  • 2013-06-11
  • 1970-01-01
  • 2018-11-11
相关资源
最近更新 更多