简体   繁体   English

活动之间丢失对象

[英]Losing objects between activities

Im passing Serializable objects between activities, but seems that somehow im loosing some of them. 我在活动之间传递了可序列化的对象,但似乎以某种方式失去了它们。 Here is what I do: 这是我的工作:

I parse the objects from json and all is good when I check them. 我从json解析对象,当我检查它们时一切都很好。 After parsing I pass them to another activity by doing this: 解析后,我通过执行以下操作将它们传递给另一个活动:

 Intent intent = new Intent(Splash.this, Main.class);
 intent.putExtra(Constants.MAIN_ARR_OBJ, mainObjs);
 startActivity(intent);
 finish();

Now in my second activity I put them in a list, and onItemClick I want to send the object coresponding to the position clicked to another activity by doing: 现在在第二个活动中,将它们放在列表中,然后onItemClick我想通过执行以下操作将与点击位置对应的对象核心发送到另一个活动:

Intent intent = new Intent(Main.this, Second.class);
Log.v("--", mainObjects.get(position).getAddi_info().getTasks()
.size() + " TASKS SIZE IN MAIN toward second ");
intent.putExtra(Constants.ADDI_INFO_OBJ,
mainObjects.get(position).getAddi_info());
startActivity(intent);

And as you see I do a check using Log.v... but that gives me size of 0 - that means there is no objects in the arrayList. 如您所见,我使用Log.v...进行了检查Log.v...但是大小为0-这意味着arrayList中没有对象。 In the second activity I get extras by doing 在第二个活动中,我通过做得到额外的东西

mainObjects = (ArrayList<MainScreenObject>) getIntent()
.getSerializableExtra(Constants.MAIN_ARR_OBJ);

EDIT: In the second activity I get the main object, but that object holds an arrayList of objects, those objects are missing. 编辑:在第二个活动中,我得到了主要对象,但是该对象持有对象的arrayList,这些对象丢失了。

Anyone got a clue what can the problem be? 任何人都知道这可能是什么问题?

In your first activity : 在您的第一次活动中:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                            intent.putExtra("login", jObj.getString(KEY_LOGIN));
                            intent.putExtra("mdp", jObj.getString(KEY_MDP));
                            intent.putExtra("prenom", jObj.getString(KEY_PRENOM));
                            intent.putExtra("nom", jObj.getString(KEY_NOM));
                            intent.putExtra("mail", jObj.getString(KEY_MAIL));
                            intent.putExtra("tel", jObj.getString(KEY_TEL));
                            startActivity(intent);

In your second activity : 在第二项活动中:

Intent intent = getIntent();

          if (intent != null) {
              login = intent.getStringExtra("login");
              mdp = intent.getStringExtra("mdp");

              items.add(intent.getStringExtra("login"));
              items.add(intent.getStringExtra("prenom"));
              items.add(intent.getStringExtra("nom"));
              items.add(intent.getStringExtra("mail"));
              items.add(intent.getStringExtra("tel"));
           }

问题是我在for周期中读取json时正在重置第二个对象arraylist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM