java.lang.NullPointerException:尝试调用虚方法' void android.widget.EditText.setT

时间:2016-06-26 12:58:31

标签: java android

我从代码的最后一行收到错误。该应用程序停止工作。 错误:

  

java.lang.NullPointerException:尝试调用虚方法' void android.widget.EditText.setText(java.lang.CharSequence)'在null对象引用上。

我的代码如下:

package com.cal.sam.calculator;

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

public class MainActivity extends AppCompatActivity {
    Button buttonCal;

    EditText feet1, feet2, feet3, inch1, inch2, inch3, res;

    float mFeet1, mInch1, mFeet2, mInch2, mFeet3, mInch3, len, wid, hig = 0;

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

        buttonCal = (Button) findViewById(R.id.buttonCal);

        feet1 = (EditText) findViewById(R.id.feet1);
        feet2 = (EditText) findViewById(R.id.feet2);
        feet3 = (EditText) findViewById(R.id.feet3);
        inch1 = (EditText) findViewById(R.id.inch1);
        inch2 = (EditText) findViewById(R.id.inch2);
        inch3 = (EditText) findViewById(R.id.inch3);

        buttonCal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mFeet1 = Float.parseFloat(feet1.getText() + "");
                mFeet2 = Float.parseFloat(feet2.getText() + "");
                mFeet3 = Float.parseFloat(feet3.getText() + "");
                mInch1 = Float.parseFloat(inch1.getText() + "");
                mInch2 = Float.parseFloat(inch2.getText() + "");
                mInch3 = Float.parseFloat(inch3.getText() + "");
                len = ((mFeet1 * 12) + mInch1);
                wid = ((mFeet2 * 12) + mInch2);
                hig = ((mFeet3 * 12) + mInch3);
                res.setText((len * wid * hig) + " ");
            }
        });
    }
}

0 个答案:

没有答案