java - 单击 android studio 时单击按钮崩溃

标签 java android

我正在尝试制作一个包含学生详细信息的表单,并再次在 TextView 中显示该详细信息。当用户在编辑文本中键入详细信息时,将它们保存到模型类中,然后在 TextView 中再次显示它们。首先,我创建了一个模型类来保存学生的变量。

日志猫:

2021-11-09 13:12:47.784 30040-30040/com.example.studentapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.studentapp, PID: 30040
java.lang.NumberFormatException: For input string: "stephen"
    at java.lang.Integer.parseInt(Integer.java:608)
    at java.lang.Integer.parseInt(Integer.java:643)
    at com.example.studentapp.MainActivity$1.onClick(MainActivity.java:52)
    at android.view.View.performClick(View.java:6291)
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
    at android.view.View$PerformClick.run(View.java:24931)
    at android.os.Handler.handleCallback(Handler.java:808)
    at android.os.Handler.dispatchMessage(Handler.java:101)
    at android.os.Looper.loop(Looper.java:166)
    at android.app.ActivityThread.main(ActivityThread.java:7529)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

XML 文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edit_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/name"

        android:autofillHints="" />

    <EditText
        android:id="@+id/edit_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Age"

        android:autofillHints="" />

    <EditText
        android:id="@+id/edit_grade"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Grade"

        android:autofillHints="" />

    <EditText
        android:id="@+id/edit_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Address"

         />

    <EditText
        android:id="@+id/edit_distance"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Distance"

         />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="50dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:paddingTop="10dp"
            android:paddingBottom="10dp" />

        <TextView
            android:id="@+id/txtAge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:paddingTop="10dp"
            android:paddingBottom="10dp" />

        <TextView
            android:id="@+id/txtGrade"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:paddingTop="10dp"
            android:paddingBottom="10dp" />

        <TextView
            android:id="@+id/txtAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:paddingTop="10dp"
            android:paddingBottom="10dp" />

        <TextView
            android:id="@+id/txtDistance"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:paddingTop="10dp"
            android:paddingBottom="10dp" />
        
        <Button
            android:text="View"
            
            android:id="@+id/btnView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />

        
    </LinearLayout>

</LinearLayout>

模型类:

package com.example.studentapp;
   public class Student {
   public String name = "";
   public int age = 0;
   public int grade = 0;
   public String address = "";
   public double distance = 0;
}

这是 .java 文件:

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

            Student student1 = new Student();
            student1.name = editName.getText().toString();
            student1.age = Integer.parseInt(editAge.getText().toString());
            student1.grade = Integer.parseInt( editName.getText().toString());
            student1.address = editName.getText().toString();
            student1.distance = Double.parseDouble( editName.getText().toString());

            textName.setText(student1.name);
            textAge.setText(""+student1.age);
            textGrade.setText(student1.grade+" Years");
            textAddress.setText(student1.address);
            textDistance.setText(student1.distance+"km");

        }
    });
  }
}

请记住,我是 Android 开发的初学者。

最佳答案

student1.distance = Double.parseDouble(editName.getText().toString());

您正在使用editName来获取距离等级和地址。请根据您的需要更改它们。

关于java - 单击 android studio 时单击按钮崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69893904/

相关文章:

java - 为什么我的几何图形不会出现? (jMonkey引擎)

java - 在 Android Fragment 中实现 Back Pressed

java - 无法在 Android 上初始化 UART 服务

java - 使用 DateFormat 时,am/pm 字符串未本地化/翻译

java - IF 条件在 Java Selenium 中不起作用

java - 编写一个计算所得税的方法。变量未初始化

android - gmail-api 标记为 "read"

java - 加密消息但无法在 android 中解密

android - 更新 Material 抽屉导航标题

android - root.listFiles() 返回 null