基本计算器android开发失败

时间:2013-07-06 09:35:45

标签: java android nullpointerexception

我正在按照有关创建温度转换器的教程进行操作。然后我想让我开发一个计算器(基本编辑文本,文本视图和按钮操作)来使用这些小部件进一步练习。

我写了下面的代码,但由于某种原因它失败了。我相信当传递给result.setText()时它是一个NullPointerException但我甚至在那里硬编码了一个字符串,但它不起作用。第一个代码是我的代码而另一个代码是转换器,它对我很好:

public class MainActivity extends Activity {


    private EditText firstNumber;
    private EditText secondNumber;
    private TextView result;

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

    public void calculate(View view)
    {
        switch(view.getId())
        {
        case R.id.additionButton:
            firstNumber = (EditText) findViewById(R.id.editText1);
            secondNumber = (EditText) findViewById(R.id.editText2);
            if(firstNumber.getText().length() != 0 && secondNumber.getText().length() != 0)
            {
                float x = Float.parseFloat(firstNumber.getText().toString());
                float y = Float.parseFloat(secondNumber.getText().toString());
                result.setText(String.valueOf(addition(x, y)));
            }
         break; 
        }
    }

    private float addition(float x, float y)
    {
        return x + y;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

和转换器代码:

public class MainActivity extends Activity {
      private EditText text;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (EditText) findViewById(R.id.editText1);

      }

      // This method is called at button click because we assigned the name to the
      // "OnClick property" of the button
      public void Convert(View view) {
        switch (view.getId()) {
        case R.id.button1:
          RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
          RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
          if (text.getText().length() == 0) {
            Toast.makeText(this, "Please enter a valid number",
                Toast.LENGTH_LONG).show();
            return;
          }

          float inputValue = Float.parseFloat(text.getText().toString());
          if (celsiusButton.isChecked()) {
            text.setText(String
                .valueOf(convertFahrenheitToCelsius(inputValue)));
            celsiusButton.setChecked(false);
            fahrenheitButton.setChecked(true);
          } else {
            text.setText(String
                .valueOf(convertCelsiusToFahrenheit(inputValue)));
            fahrenheitButton.setChecked(false);
            celsiusButton.setChecked(true);
          }
          break;
        }
      }

      // Converts to celsius
      private float convertFahrenheitToCelsius(float fahrenheit) {
        return ((fahrenheit - 32) * 5 / 9);
      }

      // Converts to fahrenheit
      private float convertCelsiusToFahrenheit(float celsius) {
        return ((celsius * 9) / 5) + 32;
      }
    } 

请帮助我,因为我正在开始android开发:)非常感谢!

注意:我说我相信因为我还习惯调试。这是它显示的错误信息:

  

07-06 12:31:39.236:E / AndroidRuntime(28638):引起:   java.lang.NullPointerException 07-06 12:31:39.236:   E / AndroidRuntime(28638):at   com.khalid.basiccalulator.MainActivity.calculate(MainActivity.java:34)

3 个答案:

答案 0 :(得分:4)

private TextView result;

您从未初始化TextView结果。

result = (TextView) findViewById(R.id.idofresulttextview);

idofresulttextview必须在activity_main.xml布局中声明,并且必须是TextView

答案 1 :(得分:2)

TextView result未初始化。

setContentView()方法中onCreate()之后,您应该初始化result变量。代码应该是这样的:

result = (TextView) findViewById(R.id.textView);

答案 2 :(得分:2)

您应该初始化字段result

private TextView result;

result =  ??