【问题标题】:Android getIntent().getExtras() returns nullAndroid getIntent().getExtras() 返回 null
【发布时间】:2011-05-10 02:22:54
【问题描述】:

我试图在两个活动之间传递一个字符串。我在其他项目中使用相同的方法完成了此操作,但由于某种原因,当我调用 intent.getStringExtra(String) 时,我得到了 NullPointerException。我还尝试通过

为附加功能创建一个 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);
    }

这是活动 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);

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

【问题讨论】:

  • 你是怎么解决这个问题的?
  • 另一种解决方案:getExtras().get("sharedString")

标签: java android


【解决方案1】:

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

【讨论】:

  • 这也解决了我的问题,因为我传入 EditText.getText() 这是一个 Editable 对象,即使 putExtra() 需要两个字符串,编译器也不会抱怨传递给它一个 Editable对象。
  • 您需要为 EditText.getText() 返回的 Editable 对象放置 String.valueOf() 的原因是它将 direclt 映射到 Serializable,因此使用了 putExtra(String, Serializable),因此需要调用 getSerializableExtra(String)。
  • \|/(>_
  • 能否请您编辑解释一下为什么这是必要的?既然select已经声明为字符串了,为什么还要调用toString呢?
  • 那么我们应该放置toString() 的原因是什么,因为select 已经被声明为一个字符串
【解决方案2】:

幸运的是我找到了这篇文章。我已经为此苦苦挣扎了几个小时,最后我意识到我也在将我的意图发送给 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);

【讨论】:

    【解决方案3】:

    试试下面的代码。我已经在你的代码中添加了一个覆盖,这可以解决问题:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 不走运。我应该更具体地说明我发布的活动 B 实际上是 EightsOnPylons.java。此外,打印“OUTBOUND INTENT”的活动一中的 Log.d 语句确实显示了正确的字符串,所以我很确定额外的内容实际上是按照意图发送的。
      • 发现问题。这是程序设计的问题,而不是代码的问题。 Activity B 是一个名为 Activity C 的 tabhost。我试图从 Activity C 中提取额外的,但额外的只是从 A 发送到 C。
      【解决方案5】:

      如果你还没有在包里放任何东西,它总是会返回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)
      

      【讨论】:

      • 我认为第一个案例可以在进程死亡后为空。第二个选项不会为空。出于这个原因,我不久前将那个虚拟变量称为__EXISTENCE_HOOK__
      【解决方案6】:

      当我发现在使用 putExtra() 之前不使用 startAcitvity() 时,我的问题得到了解决。 新手错误,但我希望有人会发现它有用。

      我改了

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

      到这里

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

      【讨论】:

        【解决方案7】:

        在活动 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");      
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多