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

Android Studio中点击按钮崩溃问题:学生信息表单展示功能异常排查

解决Android学生信息表单点击View按钮崩溃的问题

嘿,我看到你作为Android新手遇到的这个崩溃问题了,咱们直接从根源入手解决它~

崩溃原因分析

从你提供的Logcat报错信息可以看到:

java.lang.NumberFormatException: For input string: "stephen"

这个错误说明你在尝试把一个非数字的字符串(这里是名字"stephen")转换成整数类型,而这在Java里是不允许的。咱们看你的MainActivity里的onClick方法,能发现明显的错误:你复制粘贴代码的时候,错误地把editName的文本复用给了grade、address和distance的赋值,而不是使用对应的editGrade、editAddress、editDistance。

具体修复步骤

1. 修正EditText的引用错误

把onClick里错误的EditText引用替换成对应的控件:

  • 给student1.grade赋值时,应该用editGrade.getText().toString(),而不是editName的文本
  • 给student1.address赋值时,应该用editAddress.getText().toString()
  • 给student1.distance赋值时,应该用editDistance.getText().toString()

2. 添加输入验证与异常捕获

作为新手要养成一个好习惯:永远不要假设用户会输入正确格式的内容。我们需要添加非空检查,并且捕获转换数字时可能出现的异常,避免应用崩溃。

3. 优化TextView的显示文本

比如textGrade的内容写成student1.grade+" 年级"比+" Years"更合理,年龄的显示也可以调整得更清晰。

修改后的完整MainActivity代码

package com.example.studentapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button buttonView;
    EditText editName;
    EditText editAge;
    EditText editGrade;
    EditText editAddress;
    EditText editDistance;
    TextView textName, textAge, textGrade, textAddress, textDistance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editName = findViewById(R.id.edit_name);
        editAge = findViewById(R.id.edit_age);
        editGrade = findViewById(R.id.edit_grade);
        editAddress = findViewById(R.id.edit_address);
        editDistance = findViewById(R.id.edit_distance);

        textName = findViewById(R.id.txtName);
        textAge = findViewById(R.id.txtAge);
        textGrade = findViewById(R.id.txtGrade);
        textAddress = findViewById(R.id.txtAddress);
        textDistance = findViewById(R.id.txtDistance);

        buttonView = findViewById(R.id.btnView);
        buttonView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 先获取所有输入内容并去除首尾空格
                String name = editName.getText().toString().trim();
                String ageStr = editAge.getText().toString().trim();
                String gradeStr = editGrade.getText().toString().trim();
                String address = editAddress.getText().toString().trim();
                String distanceStr = editDistance.getText().toString().trim();

                // 非空检查,提示用户填写完整信息
                if (name.isEmpty() || ageStr.isEmpty() || gradeStr.isEmpty() || address.isEmpty() || distanceStr.isEmpty()) {
                    Toast.makeText(MainActivity.this, "请填写所有信息", Toast.LENGTH_SHORT).show();
                    return;
                }

                Student student1 = new Student();
                student1.name = name;

                try {
                    // 安全转换数字类型并赋值
                    student1.age = Integer.parseInt(ageStr);
                    student1.grade = Integer.parseInt(gradeStr);
                    student1.distance = Double.parseDouble(distanceStr);
                } catch (NumberFormatException e) {
                    // 捕获数字转换异常,提示用户输入正确格式
                    Toast.makeText(MainActivity.this, "年龄、年级请输入整数,距离请输入有效数字", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                    return;
                }

                student1.address = address;

                // 格式化展示信息到TextView
                textName.setText("姓名:" + student1.name);
                textAge.setText("年龄:" + student1.age + " 岁");
                textGrade.setText("年级:" + student1.grade);
                textAddress.setText("地址:" + student1.address);
                textDistance.setText("距离:" + student1.distance + " km");
            }
        });
    }
}

额外优化建议

你还可以在XML布局里给数字类型的EditText添加输入类型限制,这样用户只能输入数字,从源头减少错误:

<!-- 年龄输入框 -->
<EditText
    android:id="@+id/edit_age"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Age"
    android:inputType="number"
    android:autofillHints="" />

<!-- 年级输入框 -->
<EditText
    android:id="@+id/edit_grade"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Grade"
    android:inputType="number"
    android:autofillHints="" />

<!-- 距离输入框 -->
<EditText
    android:id="@+id/edit_distance"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Distance"
    android:inputType="numberDecimal" />

这样修改后,你的应用点击View按钮就不会崩溃了,还能给用户友好的输入提示~

内容的提问来源于stack exchange,提问作者Chanuka Jayamal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.01 01:52:42