2011-10-19 145 views
2

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

活動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; 
    } 

回答

1

您必須使用發送精確關鍵字:

String hotelName = b.getString("hotelname"); 

更新!

+0

嗨,我試過,但它仍然爲空。我以爲b.getString(「hotelname」);從捆綁中獲得了關鍵的酒店名稱?我將toBundle方法添加到原始文章 – sam

+0

好了,現在,將所有相同的字符串'hotelname'設爲相同的字符串,沒有大寫字母,也沒有空格,使用精確的字符串作爲名稱'Hotel Name' –

0

使用Intent.putExtra(Bundle)方法。

intent.putExtra(b); 
0

而不是使用您的toBundle方法,你應該implementParcelable,和你的對象寫入Intent

2
Bundle b = new Bundle(); 
b = toBundle(app.getHotelList().get(position)); 
intent.putExtras(b); 
startActivity(intent); 




//In your next activity 

Bundle bundle = getIntent().getExtras(); 
if(bundle!=null){ 
String hotelName = b.getString("hotelname"); 
} 
0

活動A(送包): intent.putExtra("Hotel Bundle", b);

活動B(裝載包): getintent().getExtra("Hotel Bundle");

getintent().getExtra*s*

0

我不知道是否有人仍然需要這但是,在你的intent.putExtra("Hotel Bundle", b);

這是不是你發只是intent.putExtra(b);這樣你就可以通過

Bundle b = this.getIntent().getExtras(); 
String hotelName = b.getString("hotelname"); 

獲取數據,或者如果你想保持Hotel Bundle關鍵

Bundle b = this.getIntent().getBundleExtra("Hotel Bundle"); 
String hotelName = b.getString("hotelname"); 

對不起,我需要這裏的答案,那麼不妨幫助那些誰遇到了這個。乾杯。

相關問題