【问题标题】:Null data when passing ArrayList<Integer> through Intent通过 Intent 传递 ArrayList<Integer> 时的空数据
【发布时间】:2014-07-10 04:01:38
【问题描述】:

我确定我错过了一些愚蠢的事情,这不可能这么难......

我正在将一个整数数组列表从一个类传递到另一个类。日志显示传递类中的数据是正确的,但在接收类中总是显示为空。 所有其他意图数据均已正确传递。

ArrayList unsavedEditedSets 是私有类变量。

请帮忙。

通过类段:

holder.returnButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v)
        {
            unsavedEditedSets.add(setPositionChoice);
            Intent i = new Intent(SetEditor.this, DisplayFullWorkout.class);
            i.putIntegerArrayListExtra("use", unsavedEditedSets); 

            Log.d("INIT - OK",""+unsavedEditedSets); // This shows data is in the arraylist

            i.putExtra("subsets", subsetsList);
            i.putExtras(extras);
            startActivity(i);      
        } 
    });

捕捉类:

private ArrayList<Integer> unsavedEditedSets;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_full_workout);

    Intent incomingI = getIntent();
    subsetsList = (ArrayList<HashMap<String, String>>)incomingI.getSerializableExtra("subsets");
    unsavedEditedSets = (ArrayList<Integer>) incomingI.getIntegerArrayListExtra("use");

    extras = incomingI.getExtras(); 

  Log.d("Incoming - BAD",""+unsavedEditedSets); // This shows null arraylist        
}

在发送前尝试使用新的数组列表,但仍然为空:

   ArrayList<Integer> test = new ArrayList<Integer>();
   test.add(12);
   test.add(19);
   i.putExtra("use", test); Log.d("INIT - OK",""+test); 

【问题讨论】:

  • 日志仅显示:Init - OK [1] 和 Incoming - Bad null
  • 尝试传递相同的“子集”意味着 SerializableExtra。
  • 谢谢@Haresh,我原来是这样的,它返回null。刚刚又试了一次,以防万一……还是空。
  • 能否提供捕获类中unsavedEditedSets的定义?
  • 如果删除putExtrasputExtra("subsets"...) 会怎样? extras 是否偶然包含"use"

标签: android android-intent arraylist


【解决方案1】:

修改为我对帖子的评论的答案:

简化代码以仅传递作为 null 传入的 use extra。看看这是否有效。如果是这样;然后加回 subsets 额外的。

extras 对象有可能(并且基于它在没有的情况下工作)已经有一个额外的键 use

另一项检查是将密钥从 use 更改为新值 (not_used_elsewhere_cause_its_really_long),并查看在放回 putExtras 时是否成功传递了该值。

【讨论】:

  • 更新。 Nija 的回答让我得到了解决,并且对于大多数有这个问题的人来说可能是正确的。但是,如果其他人犯了同样的错误,我的问题是我忘记在传递类中实现 Serializable。这是在传递的 非序列化 列表中传递空项的直接原因。奇怪的表现方式,很难追踪。最后,确实是我的愚蠢错误,但不容易找到。再次感谢大家。
【解决方案2】:

如果我没记错的话,应该是这样的:

Bundle extras = incomingIntent.getExtras();
ArrayList<Integer>  = extras.getIntegerArrayList("key");

【讨论】:

  • 感谢 J.Romero。仍然收到相同的空值...我通常会因为一些愚蠢的事情而责备自己,但是在意图之前直接添加的值对其进行测试似乎也应该起作用...
猜你喜欢
  • 2011-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
相关资源
最近更新 更多