OnClickListener上的空对象引用?

时间:2018-03-13 06:09:33

标签: java android xml android-studio

我正在使用Android Studio,我试图制作这个"化学计算器"应用程序,我收到此错误。基本上,一旦你进入应用程序,你有几个选项,你想要做什么样的计算。如果你点击"摩尔浓度",你会得到2个选项。单击类名"MolarConcentration2"下的选项会停止应用,然后崩溃...与第29行有关,即calcBtnBTN.setOnClickListener(new View.OnClickListener() {。我在那里有一个onclicklistener,你将在下面看到。事实上,对于你可以选择的另一个选项而言,我的字面意思完全相同(名称改变了一点)。

这是第二个活动页面,它提供了2个选项。 MolarConcentration类工作正常,它的MolarConcentration2类给出了问题

package com.example.swapn.chemistrycalculator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class iHaveForMolarConcentration extends AppCompatActivity {

    Button haveMMV;
    Button haveMV;

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

        haveMMV = (Button) findViewById(R.id.haveMassAndGMMandV);
        haveMV = (Button) findViewById(R.id.haveMandV);

        haveMMV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openMMV();
            }
        });

        haveMV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openMC();
            }
        });
    }

    private void openMC() {
        Intent goToMolarConcentration = new Intent(iHaveForMolarConcentration.this, MolarConcentration.class);
        startActivity(goToMolarConcentration);
    }

    private void openMMV() {
        Intent goToMolarConcentration2 = new Intent(iHaveForMolarConcentration.this, MolarConcentration2.class);
        startActivity(goToMolarConcentration2);
    }
}

这是logcat:

03-13 02:00:28.535 21718-21718/com.example.swapn.chemistrycalculator E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       Process: com.example.swapn.chemistrycalculator, PID: 21718
                                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.swapn.chemistrycalculator/com.example.swapn.chemistrycalculator.MolarConcentration2}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
                                                                                           at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6688)
                                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                                                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                           at com.example.swapn.chemistrycalculator.MolarConcentration2.onCreate(MolarConcentration2.java:29)
                                                                                           at android.app.Activity.performCreate(Activity.java:6912)
                                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008) 
                                                                                           at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) 
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                           at android.os.Looper.loop(Looper.java:154) 
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6688) 
                                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

最后,这里是类MolarConcentration2,它有问题@第29行:

package com.example.swapn.chemistrycalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MolarConcentration2 extends AppCompatActivity {

    EditText massET;
    EditText volumeET;
    EditText gmmET;
    Button calcBtnBTN;
    EditText theAnswer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        massET = findViewById(R.id.mass);
        volumeET = findViewById(R.id.volume);
        gmmET = findViewById(R.id.molecularWeight);
        calcBtnBTN = findViewById(R.id.calcBtn);
        theAnswer = findViewById(R.id.theAnswer);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_molar_concentration2);

        calcBtnBTN.setOnClickListener(new View.OnClickListener() {<--THIS LINE HERE
            @Override
            public void onClick(View v) {

                float m = Float.parseFloat(massET.getText().toString());
                float volume = Float.parseFloat(volumeET.getText().toString());
                float molcWt = Float.parseFloat(gmmET.getText().toString());

                float mols = m / molcWt;
                float nDivV = mols / volume;

                theAnswer.setText(Float.toString(nDivV));

            }
        });


    }
}

以下是MolarConcentration2类的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.swapn.chemistrycalculator.MolarConcentration2">

    <EditText
        android:id="@+id/mass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Mass (g)"
        android:inputType="numberDecimal"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.24" />

    <EditText
        android:id="@+id/molecularWeight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Molecular Weight (GMM)"
        android:inputType="numberDecimal"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.342" />

    <EditText
        android:id="@+id/volume"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Volume (L)"
        android:inputType="numberDecimal"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.447" />

    <Button
        android:id="@+id/calcBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Find Concentration"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.554" />

    <EditText
        android:id="@+id/theAnswer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Concentration (mol/L)"
        android:inputType="numberDecimal"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.659" />

</android.support.constraint.ConstraintLayout>

感谢您的帮助

0 个答案:

没有答案