You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中表单信息显示为null的问题排查求助

Android注册表单数据传递后结果页面显示null问题排查

问题描述

编写注册表单信息展示代码,将注册表单数据传递到结果页面显示,但填写表单信息后,ResultActivity显示的结果均为null,需排查问题。

相关代码

RegisterButtonActivity.java

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

        }
    });
}

ResultActivity.java

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

问题排查与修复

1. Intent Extra键名不匹配(核心问题)

在RegisterButtonActivity中,putExtra的键名末尾都带有空格(如"name "),但在ResultActivity中取值时使用的是不带空格的键名(如"name"),键名完全一致才能正确获取数据。

修复方式:去掉putExtra键名后的空格,保持和取值时的键名一致:

// 修改RegisterButtonActivity中的putExtra代码
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);

2. CheckBox状态判断错误

代码中使用chk.isSelected()判断CheckBox是否勾选,这是错误的。isSelected()是View的通用选中状态,而CheckBox的勾选状态需要用isChecked()方法判断。

修复方式:替换为isChecked():

// 修改CheckBox状态判断代码
Conditions = chk.isChecked();

3. RadioButton空指针风险

若用户未选择性别,genderRadioGroup.getCheckedRadioButtonId()会返回-1,此时findViewById(genderId)得到null,调用getText()会抛出空指针异常。

修复方式:添加非空判断:

// 修改性别获取代码
genderId = genderRadioGroup.getCheckedRadioButtonId();
String gender = "未选择"; // 默认值
if (genderId != -1) {
    genderRadioButton = findViewById(genderId);
    gender = genderRadioButton.getText().toString();
}

4. 优化建议:提前初始化View

当前代码在按钮点击事件中重复调用findViewById,可以在onCreate中一次性初始化所有View,避免重复查找,提升性能:

// 在RegisterButtonActivity的onCreate中初始化View
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register_button);

    // 提前初始化所有View
    btn = findViewById(R.id.reg_button);
    Name = findViewById(R.id.NameText);
    LastName = findViewById(R.id.LastNameText);
    Email = findViewById(R.id.EmailText);
    Password = findViewById(R.id.PasswordText);
    genderRadioGroup = findViewById(R.id.RadGrp);
    chk = findViewById(R.id.CheckBoxConditions);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // 直接获取值即可,无需再findViewById
            String name = Name.getText().toString();
            String lastname = LastName.getText().toString();
            // ... 后续代码
        }
    });
}

内容的提问来源于stack exchange,提问作者MMd.NrC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 01:52:57