android – 创建包并发送到新活动

前端之家收集整理的这篇文章主要介绍了android – 创建包并发送到新活动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在一个活动中创建一个包,然后在另一个活动中提取

这是它在主要活动中创建的时间

  1. //Create bundle to reference values in next class
  2. Bundle bundle = new Bundle();
  3. bundle.putInt("ODD",odd);
  4. bundle.putInt("EVEN",even);
  5. bundle.putInt("SMALL",small);
  6. bundle.putInt("BIG",big);
  7. //After all data has been entered and calculated,go to new page for results
  8. Intent myIntent = new Intent();
  9. myIntent.setClass(getBaseContext(),Results.class);
  10. startActivity(myIntent);
  11. //Add the bundle into myIntent for referencing variables
  12. myIntent.putExtras(bundle);

然后,当我提取其他活动

  1. //Extract the bundle from the intent to use variables
  2. Bundle bundle = getIntent().getExtras();
  3. //Extract each value from the bundle for usage
  4. int odd = bundle.getInt("ODD");
  5. int even = bundle.getInt("EVEN");
  6. int big = bundle.getInt("BIG");
  7. int small = bundle.getInt("SMALL");

当我在第二个活动中提取包时应用程序崩溃.
但是当我注释掉束的提取时.该应用运行正常.所以我把它缩小到了那个范围.

我的日志猫并没有真正解释错误是什么,或者我只是不明白它

想法?

解决方法

您在调用startActivity(myIntent)后添加以下代码;
  1. //Add the bundle into myIntent for referencing variables
  2. myIntent.putExtras(bundle);

把它放在startActivity(myIntent)之前;

猜你在找的Android相关文章