我试图从一个活动发送包到另一个活动。当我在接收活动中加载包时,所有信息似乎都是空的。下面是一些代码:将包发送到另一个活动
活动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方法只是增加字符串。我已经把日志语句放在这个方法中,并且这个bundle肯定不是null。
活动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");
}
的洛声明,然后打印 “IT IS NULL” 因为某些原因hotelName为null,但肯定是正确的密钥。
任何人都可以帮忙吗?
toBundle方法:
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;
}
嗨,我试过,但它仍然为空。我以为b.getString(“hotelname”);从捆绑中获得了关键的酒店名称?我将toBundle方法添加到原始文章 – sam
好了,现在,将所有相同的字符串'hotelname'设为相同的字符串,没有大写字母,也没有空格,使用精确的字符串作为名称'Hotel Name' –