尝试在null对象Referenc上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'

时间:2019-09-15 19:32:22

标签: java android null

我得到了公寓的数量,建筑物中的最大楼层以及一层的公寓数量。之后,我要计算公寓和门廊的楼层。这是我的main_activity

    package com.example.appartmentfloor;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    EditText floors;
    EditText apflor;
    EditText numb;
    TextView textView4;
    //TextView textView5;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        floors = findViewById(R.id.floors);
        apflor = findViewById(R.id.apflor);
        numb = findViewById(R.id.numb);
        button = findViewById(R.id.button);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String a = floors.getText().toString();
                int flors = Integer.parseInt(a);

                String b = apflor.getText().toString();
                int apflr = Integer.parseInt(b);

                String c = numb.getText().toString();
                int nmb = Integer.parseInt(c);

                int pod = 1;
                int i = 0;

                while (true) {
                    i += apflr*flors;
                    if (i>=nmb) break;
                    pod++;
                }

                String ww = Integer.toString(pod);
                textView4.setText(ww);
            }
        });
    }
}

和xml文件

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="52dp"
        android:text="Сколько в доме этажей"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/floors"
        android:layout_width="91dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="56dp"
        android:layout_marginLeft="56dp"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="44dp"
        android:text="Сколько квартир на этаже"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/apflor"
        android:layout_width="91dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="@+id/textView2"
        app:layout_constraintEnd_toEndOf="@+id/floors"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="@+id/textView2"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/button"
        android:layout_width="165dp"
        android:layout_height="61dp"
        android:layout_marginTop="28dp"
        android:clickable="true"
        android:text="Решить"
        app:layout_constraintBottom_toBottomOf="@+id/textView6"
        app:layout_constraintEnd_toEndOf="@+id/numb"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/textView6"
        app:layout_constraintTop_toBottomOf="@+id/numb"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="118dp"
        android:layout_height="45dp"
        android:layout_marginBottom="120dp"
        android:text="Ответ:"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/textView2" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="236dp"
        android:layout_height="44dp"
        android:layout_marginStart="44dp"
        android:layout_marginLeft="44dp"
        android:text="fhg"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toBottomOf="@+id/button"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="236dp"
        android:layout_height="44dp"
        android:layout_marginTop="44dp"
        android:text="fhfg"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="@+id/textView4"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/textView4"
        app:layout_constraintTop_toBottomOf="@+id/textView4"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="44dp"
        android:text="Номер квартиры"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <EditText
        android:id="@+id/numb"
        android:layout_width="91dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="@+id/textView6"
        app:layout_constraintEnd_toEndOf="@+id/apflor"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/textView6"
        app:layout_constraintTop_toTopOf="@+id/textView6"
        app:layout_constraintVertical_bias="0.523" />

当我输入所有变量并按下按钮时,它会崩溃。这是错误的文字

  

java.lang.NullPointerException:尝试调用虚拟方法'void   android.widget.TextView.setText(java.lang.CharSequence)'上的null   对象引用位于   com.example.appartmentfloor.MainActivity $ 1.onClick(MainActivity.java:51)   在android.view.View.performClick(View.java:5647)处   android.view.View $ PerformClick.run(View.java:22465)在   android.os.Handler.handleCallback(Handler.java:754)在   android.os.Handler.dispatchMessage(Handler.java:95)在   android.os.Looper.loop(Looper.java:163)在   android.app.ActivityThread.main(ActivityThread.java:6238)在   java.lang.reflect.Method.invoke(本机方法)位于   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:933)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

如果您知道哪里出了什么问题,请发短信给它))

0 个答案:

没有答案