在MainActivity中,putExtra()方法的键值对中,键名后面多了一个冒号“:”,导致在dateActivity2中获取数据时无法正确获取到对应的值。应该将键名修改为"username"、"password"和"sex"。
另外,在dateActivity2中获取数据时,使用了错误的键名,应该与MainActivity中保持一致。同时,代码中出现了未定义的变量weight,需要删除。
修改后的代码如下:
MainActivity.java
```
package com.example.mydate;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
EditText et_name,et_pwd;
RadioButton rb_boy,rb_girl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init_View();
}
void init_View(){
et_name=findViewById(R.id.et_name);
et_pwd=findViewById(R.id.et_pwd);
rb_boy=findViewById(R.id.rb_boy);
rb_girl=findViewById(R.id.rb_girl);
}
public void submit(View view) {
String name,password,sex;
name=et_name.getText().toString();
password=et_pwd.getText().toString();
if(rb_boy.isChecked()==true){
sex="男";
}else {
sex="女";
}
Intent intent=new Intent(this,dateActivity2.class);
intent.putExtra("username",name);
intent.putExtra("password",password);
intent.putExtra("sex",sex);
startActivity(intent);
}
}
```
dateActivity2.java
```
package com.example.mydate;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class dateActivity2 extends AppCompatActivity {
TextView Tv1,Tv2,Tv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date2);
Tv1=findViewById(R.id.Tv1);
Tv2=findViewById(R.id.Tv2);
Tv3=findViewById(R.id.Tv3);
Intent intent=getIntent();
String name,pwd,sex;
name=intent.getStringExtra("username");