繁体   English   中英

单击android studio时单击按钮崩溃

[英]click button crashing when click android studio

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

日志猫:

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

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

您对等级和距离使用了错误的 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());

你的代码

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()) 它不适用于非数字输入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM