【问题标题】:Android message object PendingIntent, getting null Intent.getExtras().getString("mystring")Android消息对象PendingIntent,获取null Intent.getExtras().getString("mystring")
【发布时间】:2017-06-05 12:52:38
【问题描述】:

我在点击底部栏时在活动中调用片段。 该片段在片段中有以下消息对象。

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

      Intent moreSelectionIntent = new Intent(getActivity(), MainActivity.class);
      moreSelectionIntent.putExtra("selection",Integer.toString(position));
     PendingIntent pendingIntent = PendingIntent.getActivity(getActivity().getApplicationContext(), 0, moreSelectionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
     }        ` 

片段调用activity并传​​递PendingIntent。当我使用下面的代码从意图中获取字符串时,我在活动中得到 NULL 下面是activity中的一段代码

 Intent i = this.getIntent();
    Log.i(TAG, "intenet------->"+i);

    String moreSelection = i.getExtras().getString("selection");
    Log.i(TAG, "moreSelection Bundle------->"+moreSelection);

    if(moreSelection!=null)
    { System.out.println("message"+moreSelection);}

【问题讨论】:

  • 分享您的完整代码,您是否按照您的意图添加字符串..
  • 感谢您在 stackoverflow 中鼓励其他人。我已经更新了完整的代码。

标签: android android-fragments android-intent android-activity


【解决方案1】:

您必须添加额外的字符串才能获得它。首先将其添加到您的 pendingIntent。

【讨论】:

  • 我已经创建了意图并将其添加到 pendingIntent 。
【解决方案2】:

使用此代码...

    Intent intent = new Intent(this, NextActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
    PendingIntent.FLAG_ONE_SHOT);

【讨论】:

  • 我已经更新了完整的代码。代码可能有什么问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 1970-01-01
  • 2012-04-07
  • 1970-01-01
  • 2012-11-15
  • 2020-07-27
  • 1970-01-01
相关资源
最近更新 更多