2

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

我写了下面的代码,但由于某种原因它失败了。我相信在传递给 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):在 com.khalid.basiccalulator.MainActivity.calculate (MainActivity.java:34)

4

3 回答 3

4
private TextView result;

您从未初始化 TextView 结果。

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

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

于 2013-07-06T09:36:36.200 回答
2

TextView result未初始化。

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

result = (TextView) findViewById(R.id.textView);
于 2013-07-06T09:37:51.873 回答
2

您应该初始化该字段 result

private TextView result;

result =  ??
于 2013-07-06T09:38:37.990 回答