Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Bundle在活动之间丢失值_Android_Android Intent - Fatal编程技术网

Android Bundle在活动之间丢失值

Android Bundle在活动之间丢失值,android,android-intent,Android,Android Intent,在我的第一个活动中,我想将两个字符串数组列表传递给另一个活动,但由于某种原因,当我从第二个活动中提取值时,绑定会丢失所有值。以下是发送的相关代码: NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Bundle b = new Bundle(); b.putStringArrayList("fName", friendNames

在我的第一个活动中,我想将两个字符串数组列表传递给另一个活动,但由于某种原因,当我从第二个活动中提取值时,绑定会丢失所有值。以下是发送的相关代码:

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");
names=newarraylist();
ids=新的ArrayList();
Bundle b=新Bundle();
b=getIntent().getExtras();
名称=b.getStringArrayList(“fName”);
ids=b.getStringArrayList(“fIds”);
现在,在我在第一段代码中创建通知后,我检查“friendNames”数组列表是否确实包含正确的值,并调用
b.containsKey(“fName”)
,它返回true,但只要我检查第二段代码中的“names”数组列表,这些值都不存在,当我调用
b.containsKey(“fName”)
时,它返回false。知道我做错了什么吗?

试试看

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

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
intent.putExtras(b);
 b = getIntent().getExtras();
请参阅此链接。

试一试

而不是

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
intent.putExtras(b);
 b = getIntent().getExtras();
试一试

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

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
intent.putExtras(b);
 b = getIntent().getExtras();
从您以前的活动(发送值的活动)
intent.putExtra(“android.intent.extra.intent”,b)

并在第二个活动中通过
intent.getExtra(b)

如果要交换自定义类对象,则该类必须实现
Parcelable
。如果在执行
writeString
readString
等操作时遗漏了任何成员,则相应的成员将丢失。

Hmm,仍然没有骰子。我添加了
b=getIntent().getBundleExtra(“android.intent.extra.intent”);Log.d(“此处”,b.containsKey(“fName”)+”并且我在“Log”行上得到一个空指针异常;没有骰子。我用我的原始代码和你的第一个建议尝试了你的最新建议。有什么我遗漏了,因为意图是挂起的意图的一部分吗?您最近编辑的更改挂起的意图工作得很好。当我看到你的编辑时,我偶然发现了这个权利。非常感谢。