I am trying to make a form that includes students details and again show that detail in text views. When the user types the details in edit texts save them into the model class then show them again in text views. first, I have created a model class to save variables of students.
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 file:
<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>
Model class:
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;
}
This is .java file:
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");
}
});
}
}
Please, bear in mind that I am a very beginner at android development.