2011-05-10 111 views
28

我想在两个活动之间传递一个字符串。我在其他项目中使用相同的方法完成了这项工作,但出于某种原因,当我调用intent.getStringExtra(String)时,我得到一个NullPointerException异常。我也尝试通过Android getIntent()。getExtras()返回null

Bundle b = getIntent().getExtras(); 

创建一个套件,但也返回null。以下是我目前正在尝试使用的代码。

活动答:

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); 
    } 

下面是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); 

我已经试过几乎路过群众演员的每一个替代方式,但他们都似乎表现得这样。我错过了什么?

+0

你是如何解决这个问题的? – aProgrammer 2012-05-08 10:07:34

+0

另一个解决方案:getExtras()。get(“sharedString”) – fungusanthrax 2016-08-26 23:16:30

回答

1

看看你的代码,我认为当你在Activity 1中放置任何东西时可能会发生。因为你使用了“if ... else if”,并且你没有其他情况下的“else”。如果发生这种情况,您将在活动2中获得空值。再次检查此情况。

+1

没有运气。我应该更具体地介绍我发布的活动B实际上是EightsOnPylons.java。另外,日志。在活动中打印“OUTBOUND INTENT”的d语句确实显示了正确的字符串,所以我非常肯定额外的内容实际上是以意图发送的。 – FlapsFail 2011-05-10 02:49:39

+8

发现问题。这是程序设计的问题,而不是代码。活动B是一个叫做活动C的tabhost。我试图从活动C中拿出额外的东西,但是额外的东西只能从A发送到C. – FlapsFail 2011-05-10 03:03:20

7

幸运的是,我发现这个职位。我一直在挣扎几个小时,最终我意识到我也在向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); 
11

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

+3

是的,我加了String.valueOf,它起作用! – Cheung 2013-09-12 08:26:49

+0

这也解决了我的问题,因为我传递了EditText.getText()这是一个Editable对象,即使putExtra()接受两个字符串,编译器也不会抱怨将它传递给一个Editable对象。 – glenneroo 2014-02-20 21:07:48

+1

您需要为EditText.getText()返回的Editable对象放置String.valueOf()的原因是它将direclt映射到Serializable,因此使用putExtra(String,Serializable),因此getSerializableExtra(String)需要叫做。 – TheIT 2014-08-05 20:45:51

2

如果您还没有放任何东西捆绑,它总是返回null

您可以创建并通过自己设置的包:

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) 
-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; 
} 

你正在以下面的方式使用它在交流Tivity B:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    String MANEUVER_ID = getStringExtra(savedInstanceState, "selection");  
} 
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); 
}