您的位置:首页 > 移动开发 > Android开发

android开发 -- 利用intent来传递int数据

2016-11-29 11:10 489 查看
在android系统中的intent对象是不支持直接传递int数据类型的;

如果直接传int值会报错,提示如 :Key ID expected Integer but value was a java.lang.Long.  The default value -1 was returned.

那么解决这类问题有两种方法:

方法一:
通过数据类型转换,不过在有些特殊的情况下这种方法并不适用
例如发送端:
int id=10;

Intent intent = new Intent(this, EditActivity.class);
intent.putExtra("id", id+"");      // id+"" 这样是把int转成String类型, 否则会报错
startActivity(intent);
接收端:
String sID=getIntent().getStringExtra("id");
int id=Integer.parseInt(sID);      //String转int


方法二:
通过bundle这个对象来封装数据进行传递,
例如发送端:
Bundle bundle = new Bundle();
bundle.putInt("id", 3);
intent.putExtras(bundle);
这样就可以解决问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android intent int