点击android工作室时点击按钮崩溃



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

Logcat:

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来获取距离等级和地址。请根据您的需要进行更改。

您的代码

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

预期代码

student1.grade = Integer.parseInt( editGrade.getText().toString());
student1.distance = Double.parseDouble( editDistance.getText().toString());

您正在尝试将字符串解析为整数Integer.parseInt( editName.getText().toString())。它不适用于非数字输入。

查看您的Locat消息,您似乎正在将字符串名称保存在Integer变量中,请查看您的代码库,看看您是否犯了这个错误,如果是,请将名称变量重新分配给字符串变量。

您在等级和距离方面使用了错误的editText。

您的代码:

student1.grade = Integer.parseInt(editName.getText().toString());

editName更改为editGrade

student1.grade = Integer.parseInt(editGrade.getText().toString());

以及:

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

editName更改为editDistance

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

最新更新