通过Intent传递两个值

3

我正在尝试创建一个快捷方式来启动另一个应用程序的活动(也是我的应用程序)。该快捷方式可以正确地启动活动,但只收到了两个extras中的一个(或发送了一个,我不知道)。请参见以下代码:

//Setting up the intent
Intent intent = new Intent();
intent.putExtra("num", 12);
intent.putExtra("name", "something");
intent.setComponent(new ComponentName
    ("com.myanother.app","com.myanother.app.MyActivity"));

还有另一项活动:

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        int num = extras.getInt("num"); //this worked
        String name = extras.getString("name"); //this gets null

那么,出了什么问题?如果我的英语有误,对不起。

检查一下你是否正在发送具有相同键“name”的其他数据,或者将该键更改为其他单词。 - Umer Farooq
我尝试更改键名,但它也没有起作用。而且没有其他具有相同键的数据。 - Jorge Moreira
2个回答

1

试试这个:

Bundle bund = new Bundle();

bund.putInt("num",12);
bund.putString("name","hello world");

Intent intent = new Intent();

intent.putExtras(bund);

intent.setComponent(new ComponentName
("com.myanother.app","com.myanother.app.MyActivity"));

1

不要尝试添加多个额外项,而是可以尝试将Bundle作为一个额外项传递。请参考此问题的第一个答案。该问题类似,解决方案应该有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接