简体   繁体   English

如何在意图中传递Parceable?

[英]How to pass Parceable in an intent?

I am having a problem receiving the extras that are coming in with my intent from a different activity. 我在收到来自不同活动的意图中的额外内容时遇到了问题。 When I run the code and use break points at the line where the object is actually being created I can see it being created using the debugger. 当我运行代码并在实际创建对象的行使用断点时,我可以看到它是使用调试器创建的。 I can see the object being put in the putExtra() function. 我可以看到放在putExtra()函数中的对象。

Upon arrival at the resulting activity though using the function getParceableExtra() always returns null upon receiving the intent. 在到达结果活动后,虽然使用函数getParceableExtra()在收到intent时始终返回null。

//In the receving activity //在接收活动中

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == 1){
        if(resultCode == Activity.RESULT_OK){
            Intent extras = getIntent();
            if(extras != null){
                Goals addToList = extras.getParcelableExtra(GOAL_PASSING);
                if(addToList != null)
                    mGoal.add(addToList);
            }
        }
    }
}

//In the sending activity //在发送活动中

    incrementMGoal = (findViewById(R.id.create_goal_button));
    incrementMGoal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent returnIntent = new Intent();
            Goals newGoal = new Goals(GT,GD,GDueDate);
            returnIntent.putExtra(MainActivity.GOAL_PASSING,newGoal);
            setResult(Activity.RESULT_OK,returnIntent);
            finish();
        }
    });

I expect when I create the intent to pass putExtra and then receive it to be put in a array. 我希望当我创建传递putExtra的意图然后接收它以放入数组。 I am just receiving null however. 我只是收到null。

The Problem is that your code takes the wrong intent and not of the onActivityResult parameter data 问题是您的代码采用了错误的意图而不是onActivityResult参数data

Goals addToList = data.getParcelableExtra(GOAL_PASSING);

so instead you should do this 所以你应该这样做

Goals addToList = data.getParcelableExtra(GOAL_PASSING);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM