添加两个数字Android Studio,如此简单的项目,但出现错误[重复]

Adding two numbers Android Studio, so simple project, but getting error [duplicate]

提问人:nrsharifova 提问时间:5/14/2020 最后编辑:sergiy tikhonovnrsharifova 更新时间:5/14/2020 访问量:48

问:

我正在添加 2 个数字,当单击按钮时 - 应用程序崩溃,并且此错误出现在 Android Studio 中---java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.text.Editable android.widget.EditText.getText()”

我该如何解决?

这是activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
    <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">


        <Button
            android:id="@+id/btnAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="250dp"
            android:text="Add"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.262"
            app:layout_constraintStart_toStartOf="parent" />

        <EditText
            android:id="@+id/etNum1"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_editor_absoluteY="112dp" />

        <EditText
            android:id="@+id/etNum2"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_editor_absoluteY="267dp" />

        <TextView
            android:id="@+id/txtAnswer"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:text="Answer:"
            android:textSize="32dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.895"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </LinearLayout>

这是MainActivity.java文件


package com.naila.triangle;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText num1;
    EditText num2;
    Button add;
    TextView result;

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

        num1= (EditText) findViewById(R.id.etNum1);
        num1= (EditText) findViewById(R.id.etNum2);
        add= (Button) findViewById(R.id.btnAdd);
        result= (TextView) findViewById(R.id.txtAnswer);

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int number1 = Integer.parseInt(num1.getText().toString());
                int number2 = Integer.parseInt(num2.getText().toString());
                int sum = number1 + number2;
                result.setText("Answer: "+ sum);

            }
        });




    }
}
安卓 android-layout nullpointerexception

评论


答:

0赞 codebod 5/14/2020 #1

初始化两次。更改为 .num1num1 = ...num2 = ...

0赞 Jordan Kotiadis 5/14/2020 #2

你在主类中为 num1 分配了两次值,所以 num2 不能有任何值,更改第二个变量的名称,你就可以开始了