我没有从其他活动中获取数据

时间:2018-11-19 16:00:06

标签: android android-studio android-intent toast android-toast

我在Android Studio中的项目遇到问题。 我想做的是单击一个按钮(打开另一个活动),然后在该活动中写人的数据。 然后,我保存了这些数据,应用程序回到了主目录。接下来的按钮是创建一个Toast,该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();
    }
}

//我要写入数据的下一个活动:

 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();
            }

        });
    }
}

2 个答案:

答案 0 :(得分:0)

要在完成Activitat1之后发回刚写的人员数据,您需要调用setResult,传递一个意图,然后调用finish。请在这里查看此回复以了解如何执行此操作: https://stackoverflow.com/a/947560/3342108

答案 1 :(得分:0)

btnEnviar.setOnClickListener()中,您正在创建一个新实例startActivity(intencio);,而不是返回到父活动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();        }
    });