简体   繁体   中英

basic calculator android development failing

I was following a tutorial on creating a temperature converter and it worked. Then I thought let me develop a calculator (basic edit text, text view and button manipulation) to practice further using these widgets.

I wrote the code below but it is failing for some reason. I believe it is a NullPointerException when passing to result.setText() but I even hardcoded a string there, yet it doesn't work.First code is my code and the other code is the converter that worked fine with me:

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;
    }

}

and the converter code:

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;
      }
    } 

Please help me out as I am starting android development :) Thanks a bunch!

NOTE: I said I believe because I am still getting used to debugger. This is the error msg it is showing:

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

private TextView result;

you never initialized the TextView result.

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

idofresulttextview has to be declared inside your activity_main.xml layout, and it has to be a TextView

TextView result is not initialized.

After setContentView() in your onCreate() method, you should initialize your result variable. Code should be something like:

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

You should initialize the field result

private TextView result;

result =  ??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM