安卓的getIntent().getExtras()返回null

40

我想在两个活动之间传递一个字符串。我曾在其他项目中使用相同的方法完成了这个操作,但是出现了NullPointerException错误,当我调用intent.getStringExtra(String)函数时。我也尝试通过创建一个Bundle对象来为额外的数据设置值,代码如下:

Bundle b = getIntent().getExtras();

但是它也返回了 null。以下是我目前正在尝试使用的代码。

活动 A:

Intent myIntent = null; 
    String select = "";
            if (selection.equals("Chandelle")) {
                myIntent = new Intent(Commercial.this, Chandelle.class);
                select = "Chandelle";
            } else if (selection.equals("Eights on Pylons")) {
                myIntent = new Intent(Commercial.this, EightsOnPylons.class);
                select = "Eights on Pylons";
            }
 // Start the activity
    if (myIntent != null) {
        myIntent.putExtra("selection", select);
        Log.d("*** OUTBOUND INTENT: ", "" + myIntent.getExtras().get("selection"));
        startActivity(myIntent);
    }

这是Activity B中试图获取额外信息的代码:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    Intent i = getIntent();

    if (i == null) 
        Log.d("***DEBUG****", "Intent was null");
    else
        Log.d("**** DEBUG ***", "Intent OK");

    String MANEUVER_ID  = i.getStringExtra("selection"); //Exception points to this line
    Log.d("*** DEBUG", rec + " " + MANEUVER_ID);

我已经尝试了几乎所有的传递额外信息的替代方式,但它们似乎都表现出这种行为。我错过了什么?


你是如何解决这个问题的? - Amit
另一种解决方案:getExtras().get("sharedString")。 - fungusanthrax
7个回答

14
myIntent.putExtra("selection", select); 添加 .ToString(),使其变为 myIntent.putExtra("selection", select.ToString());

这也解决了我的问题,因为我传递的是一个 Editable 对象 EditText.getText(),即使 putExtra() 接受两个字符串,编译器也不会抱怨传递一个 Editable 对象。 - glenneroo
1
你需要在EditText.getText()返回的Editable对象上使用String.valueOf()的原因是它直接映射到一个Serializable,因此使用putExtra(String, Serializable),因此需要调用getSerializableExtra(String)。 - TheIT
1
我需要一个超级英雄的名字... |/(>_<)|/... - Aiden Strydom
7
请问您能否编辑一下,解释一下为什么这是必要的呢?既然select已经被声明为字符串,他为什么还需要调用toString方法呢? - MirroredFate
那么为什么我们应该放置 toString(),既然 select 已经被声明为字符串了呢? - zihadrizkyef
toString方法返回了this。我不明白为什么这是必要的。 - jeff

8
幸运的是我找到了这篇文章。我一直在为此苦苦挣扎,最终我意识到我也把我的 intent 发送到了 tabHost 中。对于那些仍然遇到问题的人,只需从 tabHost 活动本身获取额外信息,并将其传递给子选项卡即可。
String userID;

. .

 Bundle getuserID = getIntent().getExtras();
    if (getuserID != null) {
        userID = getuserID.getString("userID");
    }

...

 intent = new Intent().setClass(this, Games.class);
    intent.putExtra("userID", userID);
    spec = tabHost.newTabSpec("games").setIndicator("Games",
                      res.getDrawable(R.drawable.tab_games))
                  .setContent(intent);
    tabHost.addTab(spec);

4
尝试下面的代码。我已经添加了一个重写到你的代码中,这将解决问题:
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState); 

   Intent i = getIntent();

   if (i == null) 
       Log.d("***DEBUG****", "Intent was null");
   else
       Log.d("**** DEBUG ***", "Intent OK");

   String MANEUVER_ID  = i.getStringExtra("selection"); //Exception points to this line
   Log.d("*** DEBUG", rec + " " + MANEUVER_ID);
}

2

看一下你的代码,我认为这可能发生在你没有在Activity 1中放置任何东西时。因为你使用了 "if...else if",而对于其他情况你没有使用 "else"。如果发生这种情况,那么在Activity 2中你将得到 null 值。请再次检查这种情况。


1
没成功。我应该在那个活动B中更加具体,我发布的实际上是EightsOnPylons.java。此外,在活动一中打印“OUTBOUND INTENT”的Log.d语句确实显示了正确的字符串,所以我非常确定extra确实随着意图被发送。 - FlapsFail
8
找到了问题所在。问题出在程序的设计上,而不是代码上。Activity B 是一个调用了 Activity C 的 Tabhost。我试图从 Activity C 中获取额外的信息,但只有从 A 发送到 C 的信息可用。 - FlapsFail

2

当我明白在使用putExtra()之前不要调用startActivity()时,我的问题得到了解决。

这是一个新手错误,但我希望有人会发现它有用。

我更改了这个:

原始答案
成为:
最初的回答

Intent i1 = new Intent(MainActivity.this,Second.class);
    startActivity(i1);
    String abc = e1.getText().toString();
    i1.putExtra("user",abc);

to this

Intent i1 = new Intent(MainActivity.this,Second.class);
    String abc = e1.getText().toString();
    i1.putExtra("user",abc);
    startActivity(i1);

2
如果您还没有将任何内容放入bundle中,它将始终返回null
您可以自己创建并设置bundle:
Intent i = new Intent( ... );

i.putExtras(new Bundle());

Bundle bundle= i.getExtras(); // (not null anymore)

或者,为了强制初始化,可以放置一个虚拟值(我更喜欢第一种解决方案):
Intent i = new Intent( ... );

i.putExtra("dummy", true); 

Bundle bundle= i.getExtras(); // (not null anymore)

我认为第一种情况在进程死亡后可能为空。第二个选项不会为空。那个虚拟变量就是我之前为此称之为 __EXISTENCE_HOOK__ 的原因。 - EpicPandaForce

-1
在B活动中,您可以使用以下函数:
  protected String getStringExtra(Bundle savedInstanceState, String id) {
    String l;
    l = (savedInstanceState == null) ? null : (String) savedInstanceState
            .getSerializable(id);
    if (l == null) {
        Bundle extras = getIntent().getExtras();
        l = extras != null ? extras.getString(id) : null;
    }
    return l;
}

你在B活动中是这样使用它的:

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState); 
   String MANEUVER_ID  = getStringExtra(savedInstanceState, "selection");      
}

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