2011-10-19 40 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"); 

对不起,我需要这里的答案,那么不妨帮助那些谁遇到了这个。干杯。

相关问题