我試圖從一個活動發送包到另一個活動。當我在接收活動中加載包時,所有信息似乎都是空的。下面是一些代碼:將包發送到另一個活動
活動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' –