Question: Need help with this Android App project. The application keeps crashing when running. It is basically an app that calculates the area and perimeter of

Need help with this Android App project. The application keeps crashing when running. It is basically an app that calculates the area and perimeter of a rectangle given the height and width entered by the user in an editText field.

activity_main.xml

                

MainActivity.java

package com.example.rectanglecalculator; import androidx.appcompat.app.AppCompatActivity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.EditorInfo; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements TextView.OnEditorActionListener{ private EditText heightField; private EditText widthField; private TextView areaCalculated; private TextView permimeterCalculated; private Button getResults; private SharedPreferences savedValues; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); heightField = (EditText)findViewById(R.id.heightField); widthField = (EditText)findViewById(R.id.widthField); areaCalculated = (TextView)findViewById(R.id.areaCalculated); permimeterCalculated = (TextView)findViewById(R.id.perimeterCalculated); getResults = (Button)findViewById(R.id.getResults); heightField.setOnEditorActionListener(this); widthField.setOnEditorActionListener(this); // create an onLister method here // remember calculate method to pass up getResults.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { calculate(); } }); } public void calculate(){ Float nWidth = Float.parseFloat(widthField.getText().toString()); Float nHeight = Float.parseFloat(heightField.getText().toString()); Float finalArea = nWidth * nHeight; areaCalculated.setText(finalArea.toString()); Float finalPerimeter = (nWidth * 2) + (nHeight *2); permimeterCalculated.setText(finalPerimeter.toString()); } @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_UNSPECIFIED){ calculate(); } return false; } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!