简体   繁体   English

为什么putextra没有传递价值?

[英]Why putextra isn't passing value?

I'm trying to send a result from my MainActivity to SecondActivity, but it always returns a default value, not the result. 我正在尝试将MainActivity的结果发送到SecondActivity,但它总是返回默认值,而不是结果。 I'm a begginer, if there is someone that could help me it would be nice. 我是个乞丐,如果有人可以帮助我,那就太好了。

I've tried everything that came to my mind, but nothing worked. 我已经尝试了所有想到的东西,但没有任何效果。

 btnPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String prvi=etPrviBroj.getText().toString().trim();
                String drugi=etDrugiBroj.getText().toString().trim();
                prviBroj=Integer.parseInt(prvi);
                drugiBroj=Integer.parseInt(drugi);
                rez=prviBroj+drugiBroj; //declared as an int and set to 0
                Intent intent=new 
Intent(MainActivity.this,SecondActivity.class);
                intent.putExtra("rez",rez);
            }
        });

//and in the second activity
rezultat=getIntent().getIntExtra("rez",0);

in first activity : 在第一项活动中:

btnSum.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (!edt1.getText().toString().isEmpty() && !edt2.getText().toString().isEmpty()) {
            sum = Integer.parseInt(edt1.getText().toString()) + Integer.parseInt(edt2.getText().toString());
            Intent intent = new Intent(Main2Activity.this, MainActivity.class);
            intent.putExtra("RESULT", sum);
            startActivity(intent);
            txtAns.setText("" + sum);
        } else if (edt1.getText().toString().isEmpty()) {
            edt1.setError("Please enter no1 ");
        } else if (edt2.getText().toString().isEmpty()) {
            edt2.setError("Please enter no2 ");

        }
    }
});

second activity 第二项活动

int sum= getIntent().getIntExtra("RESULT",0);

You can store your sum of two variables in one int variable. 您可以将两个变量的总和存储在一个int变量中。 When you click of your second button make instant of Intent and call startactivity at that time. 当您单击第二个按钮时,立即调用Intent并调用startactivity。

You're creating an Intent just to set the variable you want to pass to the second activity but in your code this Intent is not linked to the new Activity. 您正在创建一个Intent,只是为了设置要传递给第二个活动的变量,但在您的代码中,此Intent未链接到新的Activity。 If you want to pass a value between two activities with this method, you have to start the second activity with the Intent you gave the extra value to. 如果要使用此方法在两个活动之间传递值,则必须使用为其提供额外值的Intent启动第二个活动。

Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("rez",rez);
startActivity(intent);

If you want to start the second activity with another Intent you have to pass the value in another way. 如果要使用另一个Intent启动第二个活动,则必须以另一种方式传递该值。

Intent intent=new Intent(MainActivity.this,SecondActivity.class); Intent intent = new Intent(MainActivity.this,SecondActivity.class);

intent.putExtra("rez",rez); intent.putExtra( “REZ”,REZ);

startActivity(intent); startActivity(意向);

In SecondActivity Intent intent = getIntent(); 在SecondActivity Intent intent = getIntent(); String abc = intent.getStringExtra("rez"); String abc = intent.getStringExtra(“rez”);

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

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