I have written the following code in Android Studio to print the information of a register form:
package com.example.session8_project1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class RegisterButtonActivity extends AppCompatActivity {
Button btn;
EditText Name, LastName, Email, Password;
RadioGroup genderRadioGroup;
RadioButton genderRadioButton;
CheckBox chk;
int genderId;
Boolean Conditions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_button);
btn = findViewById(R.id.reg_button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chk = findViewById(R.id.CheckBoxConditions);
// Find Ids
Name = findViewById(R.id.NameText);
LastName = findViewById(R.id.LastNameText);
Email = findViewById(R.id.EmailText);
Password = findViewById(R.id.PasswordText);
// Convert to String
String name = Name.getText().toString();
String lastname = LastName.getText().toString();
String email = Email.getText().toString();
String password = Password.getText().toString();
genderRadioGroup = findViewById(R.id.RadGrp);
genderId = genderRadioGroup.getCheckedRadioButtonId();
genderRadioButton = findViewById(genderId);
String gender = genderRadioButton.getText().toString();
// Check whether the Checkboxes are selected or not.
// Conditions = chk.isChecked();
Conditions = chk.isSelected();
Intent in = new Intent(RegisterButtonActivity.this, ResultActivity.class);
in.putExtra("name ", name);
in.putExtra("lastname ", lastname);
in.putExtra("email ", email);
in.putExtra("password ", password);
in.putExtra("gender ", gender);
in.putExtra("Conditions ", Conditions);
startActivity(in);
}
});
}
}
And the activity to which this code is applying is as follows: package com.example.session8_project1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivity extends AppCompatActivity {
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Bundle extras = getIntent().getExtras();
result = findViewById(R.id.ResultText);
if (extras != null){
result.setText(extras.getString("name") + "\n" +
extras.getString("lastname" )+ "\n" +
extras.getString("email") + "\n" +
extras.getString("password") + "\n" +
extras.getString("gender") + "\n" +
extras.getBoolean("Conditions"));
}
}
}
When I fill the information of the form, it will give me the null
result. I do not know why? is there something wrong?
When you call putExtra
, you are using:
in.putExtra("name ", name);
Note that the key you are using is "name "
- with a space at the end.
When you are retrieving that extra, you are using
extras.getString("name")
With the key "name"
- without a space at the end.
Those need to match, so you should consistently use "name"
in both places.