无法从一个活动传递另一个活动的值(仅传递空值)

mnemlml8  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(324)

我试图将变量从一个活动传递到另一个活动。在main活动中,我使用toast消息调用了值。
主活动.java

search = (Button) findViewById(R.id.search);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View w) {
                subreg = spinner_subregion.getSelectedItem().toString();
                reg = spinner_region.getSelectedItem().toString();
                pettype = spinner_pet.getSelectedItem().toString();
                petnumber = spinner_num.getSelectedItem().toString();
                Intent intent = new Intent(MainActivity, Result.class);
                intent.putExtra("subregion", subreg);
                intent.putExtra("region", reg);
                intent.putExtra("petnum", petnumber);
                intent.putExtra("pet", pettype);
                startActivity(intent);

}
}

但是,当我将值传递给result.java时,它只返回null。我试图寻找解决办法,但仍然不适合我。有人知道我怎么传递数据吗?
结果.java

Bundle extras = getIntent().getExtras();
        if (extras!=null){
            subreg = extras.getString("subreg");
            reg = extras.getString("reg");
            pettype = extras.getString("pettype");
            petnumber = extras.getString("petnumber");
        }
cetgtptt

cetgtptt1#

试试这个mainactivity.java

Intent myIntent = new Intent(this, NewActivity.class);
 intent.putExtra("subregion", subreg);
 intent.putExtra("region", reg);
 intent.putExtra("petnum", petnumber);
 intent.putExtra("pet", pettype);
startActivity(myIntent)

结果.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view);

        Intent intent = getIntent();
if(intent ==null){
........
.......
}else{

        String subregion= intent.getStringExtra("subregion");
        String region= intent.getStringExtra("region");
        String petnum= intent.getStringExtra("petnum");
        String pet= intent.getStringExtra("pet");
}
    }
r7xajy2e

r7xajy2e2#

这是我修改后的代码。
主活动.java

search = (Button) findViewById(R.id.search);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View w) {
                subreg = spinner_subregion.getSelectedItem().toString();
                reg = spinner_region.getSelectedItem().toString();
                pettype = spinner_pet.getSelectedItem().toString();
                petnumber = spinner_num.getSelectedItem().toString();

                Intent intent = new Intent(Pet_sitting.this, Pet_sitting_result.class);
                intent.putExtra("subreg", subreg);
                intent.putExtra("reg", reg);
                intent.putExtra("pettype", pettype);
                intent.putExtra("petnumber", petnumber);
                startActivity(intent);

            }
        });

结果.java

Intent intent = getIntent();
        if (intent == null){
            Toast.makeText(getApplicationContext(),"Null value passed",Toast.LENGTH_SHORT).show();
        }
        else{
            subreg= intent.getStringExtra("subreg");
            reg= intent.getStringExtra("reg");
            pettype = intent.getStringExtra("pettype");
            petnumber = intent.getStringExtra("petnumber");

            Toast.makeText(getApplicationContext(),subreg + " " + reg + " " + pettype + " " + petnumber,Toast.LENGTH_SHORT).show();
        }
pprl5pva

pprl5pva3#

如我所见,您在代码中使用了微调器。所以很明显一个旋转器在旋转轮子后一次只能给你一个值。。。你需要在空值异常中设置代码。。。比如…如果它是空的,不要在intent中传递空值,也许它工作了…如果它是空的,那么回复
主活动.java

if(subreg != null){
   reg = spinner_region.getSelectedItem().toString();
   intent.putExtra("reg", reg);
 }
 startActivity(intent);

结果.java

Intent intent = getIntent();
 if (intent == null){
    Toast.makeText(getApplicationContext(),"Null value passed",Toast.LENGTH_SHORT).show();
 }else{
    reg= intent.getStringExtra("reg");
    Toast.makeText(getApplicationContext(),reg.toString,Toast.LENGTH_SHORT).show();
 }

相关问题