当前位置: 首页 > 知识库问答 >
问题:

当我试图运行我的Android应用程序[复制]时,我得到一个nullpointerx0019错误

何甫
2023-03-14

我得到这个错误

维护活动2。JAVA

public class MainActivity2 extends AppCompatActivity {

    private TextView lengteSteenInput;
    private TextView muurdikteInput;
    private TextView hoogteSteenInput;
    private TextView lagenmaatInput;
    private TextView stootvoegInput;
    private TextView aantalMInput;
    private TextView metselzand;
    private TextView voegzand;
    private TextView portlandcement;
    private TextView metselcement;
    private TextView stenen;

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

        Bundle bundle = getIntent().getExtras();

        //transporting the variables from MainActivity.java to MainActivity2.java

        lengteSteenInput = findViewById(R.id.lengteSteenInput); //this is where it says it gets the error
        muurdikteInput = findViewById(R.id.muurdikteInput);
        hoogteSteenInput = findViewById(R.id.hoogteSteenInput);
        lagenmaatInput = findViewById(R.id.lagenmaatInput);
        stootvoegInput = findViewById(R.id.stootvoegInput);
        aantalMInput = findViewById(R.id.aantalMInput);

        float lengteSteenF = bundle.getFloat("lengtesteen");
        float muurdikteF = bundle.getFloat("muurdikte");
        float hoogteSteenF = bundle.getFloat("hoogtesteen");
        float lagenmaatF = bundle.getFloat("lagenmaat");
        float aantalM2F = bundle.getFloat("aantalm2");
        String stootvoegB = getIntent().getStringExtra("stootvoeg");

        lengteSteenInput.setText(String.valueOf(lengteSteenF));
        muurdikteInput.setText(String.valueOf(muurdikteF));
        hoogteSteenInput.setText(String.valueOf(hoogteSteenF));
        lagenmaatInput.setText(String.valueOf(lagenmaatF));
        aantalMInput.setText(String.valueOf(aantalM2F));
        stootvoegInput.setText(String.valueOf(stootvoegB));

        //string -> float

        float lengteSteenInputF = Float.parseFloat(String.valueOf(lengteSteenInput));
        float muurdikteInputF = Float.parseFloat(String.valueOf(muurdikteInput));
        float hoogteSteenInputF = Float.parseFloat(String.valueOf(hoogteSteenInput));
        float lagenmaatInputF = Float.parseFloat(String.valueOf(lagenmaatInput));
        float aantalM2InputF = Float.parseFloat(String.valueOf(aantalMInput));
        boolean stootvoegInputB = Boolean.parseBoolean(String.valueOf(stootvoegInput));

活动2。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:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">

    <TextView
        android:id="@+id/hoeveelMetselzand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:text="hoeveel metselzand(inc 10% verlies):"
        android:textSize="15sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/hoeveelVoegzand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:text="benodigde hoeveelheid voegzand(20% verlies):"
        android:textSize="15sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/hoeveelMetselzand" />

    <TextView
        android:id="@+id/zakkenMetselCement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:text="benodigde zakken metselcement:"
        android:textSize="15sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/zakkenPortland" />

    <TextView
        android:id="@+id/aantalStenen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:text="aantal stenen(inc 3% verlies):"
        android:textSize="15sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/zakkenMetselCement" />

    <TextView
        android:id="@+id/zakkenPortland"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:text="benodigde zakken portlandcement:"
        android:textSize="15sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/hoeveelVoegzand" />

    <TextView
        android:id="@+id/metselzand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="15dp"
        android:text="metselzand"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/voegzand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="15dp"
        android:text="voegzand"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/metselzand" />

    <TextView
        android:id="@+id/portlandcement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="15dp"
        android:text="portlandcement"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/voegzand" />

    <TextView
        android:id="@+id/metselcement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="15dp"
        android:text="metselcement"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/portlandcement" />

    <TextView
        android:id="@+id/stenen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="15dp"
        android:text="stenen"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/metselcement" />

</androidx.constraintlayout.widget.ConstraintLayout>

有人能帮帮我吗?

共有2个答案

郎宏浚
2023-03-14

异常告诉您一切,您没有ID为“Lengtesteeningput”的Textview。在请求Android将其获取给您之前,用xml创建实际内容。

宰父焕
2023-03-14

活动\u main2中没有id为lengteSteenInput的文本视图。xml文件。

 类似资料: