【问题标题】:Android: how to pass object between activities with object contains other objectsAndroid:如何在包含其他对象的活动之间传递对象
【发布时间】:2013-07-13 00:26:10
【问题描述】:

我是 Android 的初学者,我尝试在活动之间传递对象,对象包含其他对象,但出现错误。

解决方案?

我尝试通过可序列化的对象传递:

        lALL.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View v,
                    int position, long arg3) {
                System.out.println(position);
                Locataire l = new Locataire(getResources());
                l.setNom("test");
                l.setPrenom("test");
                Intent intent = new Intent(MainActivity.this, Etat_lieux.class);
                intent.putExtra("EDL", l);
                startActivity(intent);      
            }


        });

public class Locataire implements Serializable{

private String ref;
private String civilite;
private String nom;
private String prenom;
private Contact contact = new Contact();
private Adresse adresse = new Adresse();
private String DG;
private boolean IsGarantPresent;
private boolean IsColocation;

private Resources res;

public Locataire(Resources res)
{
    this.res = res;
}

联系方式,地址

公共类Contact实现Serializable{

protected String tel;
protected String mobile;
protected String fax;
protected String email;
protected String www;

而 ressource 是第一个活动的 Context getResources()

【问题讨论】:

  • 粘贴堆栈跟踪,以便我们更好地查看错误
  • Contact,Adresse,Resources 等呢?他们是Serializable吗?
  • error is parcelable遇到ioexception写入可序列化对象资源

标签: android android-activity serializable


【解决方案1】:

如果您想在自己的应用程序中的活动之间传递数据,您应该使用startActivitystartActivityForResultonActivityResult。您创建一个 Bundle 对象并使用putExtra 将您的数据封装在其中。然后在 onCreate 方法中检索此对象。

http://developer.android.com/training/basics/intents/result.html

否则,如果您想在您的应用中的 Activity 和另一个应用中的 Activity 之间传递数据,您应该使用显式或隐式 Intent。

http://developer.android.com/training/basics/intents/sending.html

【讨论】:

    【解决方案2】:

    如果你想传递额外的对象......你必须使对象实现可序列化..并使其所有子对象实现可序列化.. 然后使用 bundle.putSerializable(OBJECT)..

    【讨论】:

      猜你喜欢
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多