2013-12-23 56 views
0

在我的第一個活動中,我想將兩個字符串數組列表傳遞給另一個活動,但由於某種原因,當我從第二個活動中提取值時,該包會丟失所有值。下面是發送相關代碼:Bundle在活動之間丟失了值

NotificationManager notificationManager = (NotificationManager) 
    getSystemService(NOTIFICATION_SERVICE); 

Bundle b = new Bundle(); 
b.putStringArrayList("fName", friendNames); 
b.putStringArrayList("fIds", friendIds); 

Intent intent = new Intent(getApplicationContext(), Friendrequest.class); 

PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 
    PendingIntent.FLAG_UPDATE_CURRENT).cancel(); 

intent.putExtras(b); 

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 

// creates notification 
Notification n = new Notification.Builder(this) 
    .setContentTitle("You have a friend request!") 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setContentIntent(pIntent).setAutoCancel(true).build(); 

notificationManager.notify(0, n); 

下面是用於接收的相關代碼:

names = new ArrayList<String>(); 
ids = new ArrayList<String>(); 

Bundle b = new Bundle(); 

b = getIntent().getExtras(); 

names = b.getStringArrayList("fName"); 

ids = b.getStringArrayList("fIds"); 

現在,經過創建我的第一個代碼段的通知,我檢查,以確保「friendNames」數組列表確實包含正確的值,我打電話給b.containsKey("fName"),它返回true,但只要我檢查代碼的第二個片段中的「names」數組列表,沒有任何值在那裏,並且當我撥打b.containsKey("fName")時,它返回false。任何想法我做錯了什麼?

回答

1

嘗試

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 
        PendingIntent.FLAG_UPDATE_CURRENT); 

,而不是

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 

參考此鏈接。 Intent.getExtras() always returns null

嘗試

intent.putExtra("android.intent.extra.INTENT", b); 

代替

intent.putExtras(b); 

,並嘗試

b = getIntent().getBundleExtra("android.intent.extra.INTENT"); 

,而不是

b = getIntent().getExtras(); 
+0

嗯,仍然沒有骰子。我加了'b = getIntent()。getBundleExtra(「android.intent.extra.INTENT」);我在「Log」行上得到了一個空指針異常。 – joebro

+0

嘗試names = savedInstanceState.getStringArrayList(「fName」); – Prem

+0

沒有骰子。我用我的原始代碼和您的第一個建議嘗試了您的最新建議。有什麼,我失蹤,因爲意圖是等待意圖的一部分? – joebro

0

將從以前的活動(這將值一)intent.putExtra("android.intent.extra.INTENT", b);

intent.getExtra(b);

0

訪問他們的第二個活動如果您正在交換自定義類的對象,則類必須實現Parcelable。如果您在執行writeStringreadString等時錯過任何會員,則相應的會員將會丟失。