意图送不原意接收原意、意图

2023-09-03 20:42:40 作者:吟雪情枫

所以,我想在一个列表中的项目后,推出了新的活动被选中.... pretty的基本依据是什么我读过。我也尝试发送的额外价值。因此,我可以选择列表中的项目,新的活动开始,额外的设置,但在演员的值是空的。我注意到在新的活动意图的ID从第1个活动的一个不匹配。我不知道这是否应该或不。

So I am trying to launch a new activity after the item in a list is selected.... pretty basic based on what I've read. I am also trying to send a value in the extras. So I can select the item in a list, and the new activity starts, extras is set, but the value in the extras is empty. I've noticed the id of the intent on the new activity doesn't match the one from the 1st activity. I don't know if it is supposed to or not.

从活动1:

public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {
                  Intent displayIntent = new Intent(getApplicationContext(), DisplayActivity.class);
              int index  = _names.indexOf(((TextView) view).getText());
              displayIntent.putExtra("ID_TAG", ids.get(index));
              startActivity(displayIntent);
          }

在活性2(DisplayActivity)

In Activity2 (DisplayActivity)

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras(); 
        _hiveIndex = extras.getLong("ID_TAG");
}

任何想法,为什么我就不能获得价值?下的额外的MMAP被设定为散列映射前的第一目的,但为空在活性2

Any Ideas why I wouldn't be getting the value? The mMap under the extras is set to a hash map before in the 1st intent, but is null in activity2.

推荐答案

活动1的未使用捆绑的对象,把包你需要使用putExtras对象displayIntent.putExtra(ID_TAG,ids.get(指数))(包束)方法,而不是it.Since你正在试图让Actvitiy2的Bundle对象。你路过比活性1 putExtra方法Bundle对象等,但你试图让Bundle对象的活性2出于这个原因,您将可以得到什么。 displayIntent.putExtra(ID_TAG,ids.get(指数));更换 displayIntent.putExtras(你的包对象)的

displayIntent.putExtra("ID_TAG", ids.get(index)) of Activity1 not using Bundle object,to put Bundle object you need to use putExtras(Bundle bundle) method instead of it.Since you are trying to get the Bundle object in Actvitiy2. Your passing other than Bundle object in Activity1 putExtra method,but you trying to get the Bundle object in Activity2 for that reason your are getting nothing. displayIntent.putExtra("ID_TAG", ids.get(index));replace with displayIntent.putExtras(your bundle object);

或者你也可以使用 getIntExtra(字符串名称,诠释设置defaultValue)方法。

Or you can use getIntExtra(String name, int defaultValue) method.