我正在尝试将一个包从一项活动发送到另一项活动。当我在接收活动中加载包时,所有信息似乎都是空的。这是一些代码:
活动A(发送包):
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Intent intent = new Intent(HotelsActivity.this, EditHotelActivity.class);
Bundle b = new Bundle();
b = toBundle(app.getHotelList().get(position));
intent.putExtra("Hotel Bundle", b);
startActivity(intent);
}
});
toBundle 方法只是将对象中的字符串添加到包中。我已将日志记录语句放入此方法中,并且该包肯定不为空。
活动 B(加载包):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.edit_hotel);
setTitleFromActivityLabel (R.id.title_text);
// Retrieve sent bundle
Bundle b = this.getIntent().getExtras();
String hotelName = b.getString("hotelname");
if (hotelName == null)
MyLog.i(TAG, "IT IS NULL");
}
然后 loggin 语句打印“IT IS NULL”,因为由于某种原因 hotelName 为 null,但这绝对是正确的键。
有人能帮忙吗?
捆绑方法:
public Bundle toBundle(HotelItem hotel) {
Bundle b = new Bundle();
b.putString("hotelname",hotel.getHotelName());
b.putString("hotel address", hotel.getHotelAddress());
b.putString("hotel telephone", hotel.getHotelTelephone());
b.putString("hotel website", hotel.getHotelWebsite());
return b;
}