intent.putExtra("UserInfo",userInfo);错误: 对于putExtra(String,UserInfo), 找不到合适的方法 方法 Intent.putExtra(String,boolean)不适用 (参数不匹配; UserInfo无法转换为boolean) 方法 Intent.putExtra(String,byte)不适用 (参数不匹配; UserInfo无法转换为byte) 方法 Intent.putExtra(String,char)不适用 (参数不匹配; Use
时间: 2024-02-20 20:58:25 浏览: 21
这个错误提示是因为您在使用Intent.putExtra()方法时,尝试将一个对象UserInfo放入Intent中,但是该方法只支持基本数据类型的存储,无法直接将对象存储进去。您需要将UserInfo对象序列化成字符串或者字节数组,再将其存储到Intent中。可以使用以下方法:
将UserInfo序列化成字符串:
```
Intent intent = new Intent();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(userInfo);
String userInfoString = bos.toString("ISO-8859-1");
intent.putExtra("UserInfo", userInfoString);
```
将UserInfo序列化成字节数组:
```
Intent intent = new Intent();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(userInfo);
byte[] userInfoBytes = bos.toByteArray();
intent.putExtra("UserInfo", userInfoBytes);
```
在接收端,需要将传递进来的字符串或字节数组反序列化成UserInfo对象,可以使用以下方法:
从字符串中反序列化出UserInfo对象:
```
String userInfoString = intent.getStringExtra("UserInfo");
ByteArrayInputStream bis = new ByteArrayInputStream(userInfoString.getBytes("ISO-8859-1"));
ObjectInputStream ois = new ObjectInputStream(bis);
UserInfo userInfo = (UserInfo) ois.readObject();
```
从字节数组中反序列化出UserInfo对象:
```
byte[] userInfoBytes = intent.getByteArrayExtra("UserInfo");
ByteArrayInputStream bis = new ByteArrayInputStream(userInfoBytes);
ObjectInputStream ois = new ObjectInputStream(bis);
UserInfo userInfo = (UserInfo) ois.readObject();
```