我没有从另一个 activity 获取数据

I'm not getting data from another activity

我在 Android Studio 中遇到项目问题。 我想做的是当我单击一个按钮(打开另一个 activity)并在那个 activity 中写入一个人的数据时。 然后我保存这些数据,应用程序回到主界面。下一个按钮所做的是创建一个 Toast,显示来自该人的数据,但 Toast 返回 null。我不知道我做错了什么。

   public class PrincipalActivity extends AppCompatActivity implements View.OnClickListener {

    Button btnObreActivitat1, btnToastDadesPersona;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);

        btnObreActivitat1 = findViewById(R.id.btnObreActivitat1);
        btnObreActivitat1.setOnClickListener(this);
        btnToastDadesPersona = findViewById(R.id.btnToastDadesPersona);
        btnToastDadesPersona.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Resposta invalida", Toast.LENGTH_SHORT);
        } else {
            switch (requestCode) {
                case 5:
                    setTitle(data.getStringExtra("RESPOSTA"));
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        Intent intencio;
        StringBuilder sbUri = new StringBuilder();

        switch (v.getId()){
            case R.id.btnObreActivitat1:
                intencio = new Intent(this,Activitat1.class);
                startActivityForResult(intencio,5);
                break;
            case R.id.btnToastDadesPersona:
                intencio = getIntent();
                String nom = intencio.getStringExtra("NOM");
                String cognom = intencio.getStringExtra("COGNOM");
                String sexe = intencio.getStringExtra("SEXE");

                Toast torrada = new Toast(this);
                LayoutInflater inflador = getLayoutInflater();
                View vista = inflador.inflate(R.layout.torrada,null);
                TextView tvMissatge = vista.findViewById(R.id.tvMissatge);
                tvMissatge.setText("Nom: " + nom + "\nCognom: " + cognom +  "\nSexe: " + sexe);

                torrada.setView(vista);
                torrada.setDuration(Toast.LENGTH_SHORT);
                torrada.show();
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }
    @Override
    protected void onResume(){
        super.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
    }
    @Override
    protected void onStop() {
        super.onStop();
    }
}

//下一个activity我写数据的地方:

 public class Activitat1 extends AppCompatActivity {
    final static String nom = "NOM";
    final static String cognom = "COGNOM";
    final static String sexe = "SEXE";

    Button btnEnviar, btnCancelar;
    EditText etNom, etCognom;
    RadioButton rbtSexe;
    RadioGroup rgpSexe;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activitat1);

        etNom = findViewById(R.id.etNom);
        etCognom = findViewById(R.id.etCognom);
        btnEnviar = findViewById(R.id.btnEnviar);
        btnCancelar = findViewById(R.id.btnCancelar);

        rgpSexe = findViewById(R.id.rgpSexe);
        rbtSexe = findViewById(rgpSexe.getCheckedRadioButtonId());
        btnEnviar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intencio = Activitat1.this.getIntent();
                intencio.putExtra(nom, etNom.getText().toString());
                intencio.putExtra(cognom, etCognom.getText().toString());
                intencio.putExtra(sexe,rbtSexe.getText().toString());
                startActivity(intencio);
            }
        });

        btnCancelar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }

        });
    }
}

要在Activitat1 完成后发回刚刚写入的persons 数据,需要调用setResult,传递一个intent,然后调用finish。请在此处查看此回复以了解如何操作:

btnEnviar.setOnClickListener() 中您正在创建一个新实例 startActivity(intencio); 而不是返回父实例 activity setResult(RESULT_OK, intencio);finish();.

试试这个

 btnEnviar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intencio = Activitat1.this.getIntent();
            intencio.putExtra(nom, etNom.getText().toString());
            intencio.putExtra(cognom, etCognom.getText().toString());
            intencio.putExtra(sexe,rbtSexe.getText().toString());
            setResult(RESULT_OK, intencio);
            finish();        }
    });