I am making a BMI calculator. In the main activity, i have radio buttons to check if the user wants metric or imperial. Recently, I incorporated a timer to determine when the user was done typing to automatically calculate their BMI. The imperial calculation works just fine, but when i click the metric radio button, it crashes right away. The error message was "Unable to start activity component info." This was not happening before. The code should be fairly similar except for the different measurements.
Thanks for all your help!
Here is the code for the metric activity:
public void performCalculation()
{
EditText weightET = (EditText) findViewById(R.id.weightET);
weightET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
private Timer timer = new Timer();
private final long DELAY = 500;
@Override
public void afterTextChanged(final Editable editable)
{
timer.cancel();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run()
{
runOnUiThread(new Runnable() {
@Override
public void run()
{
calculate();
}
});
}
}, DELAY);
}
});
}
public void calculate()
{
EditText heightET = (EditText) findViewById(R.id.height);
EditText weightET = (EditText) findViewById(R.id.weight);
TextView yourBMI = (TextView) findViewById(R.id.yourBMI);
TextView bmiClass = (TextView) findViewById(R.id.bmiClass);
double height;
double weight;
double bmi;
double bmiRoundOff;
weight = Double.parseDouble(weightET.getText().toString());
height = Double.parseDouble(heightET.getText().toString());
bmi = (weight) / (height * height);
bmiRoundOff = Math.round(bmi * 100.00) / 100.00;
yourBMI.setText("Your BMI Is " + String.valueOf(bmiRoundOff));
if(bmiRoundOff < 18.5)
{
bmiClass.setText("Your BMI Class: Underweight");
}
if(bmiRoundOff > 18.5 && bmiRoundOff <= 24.9)
{
bmiClass.setText("Your BMI Class: Normal");
}
if(bmiRoundOff >= 25.0 && bmiRoundOff <= 29.0)
{
bmiClass.setText("Your BMI Class: Overweight!");
}
if(bmiRoundOff > 29.0)
{
bmiClass.setText("Your BMI Class: Obese");
}
}
Here is the code for imperial activity:
public void performCalculation()
{
EditText weightET = (EditText) findViewById(R.id.weightET);
weightET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
private Timer timer = new Timer();
private final long DELAY = 500;
@Override
public void afterTextChanged(final Editable editable)
{
timer.cancel();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run()
{
runOnUiThread(new Runnable() {
@Override
public void run()
{
calculate();
}
});
}
}, DELAY);
}
});
}
public void calculate()
{
EditText weightET = (EditText) findViewById(R.id.weightET);
EditText feetET = (EditText) findViewById(R.id.feetET);
EditText inchesET = (EditText) findViewById(R.id.inchesET);
TextView yourBMI = (TextView) findViewById(R.id.yourBMI);
TextView bmiClass = (TextView) findViewById(R.id.bmiClass);
double feet;
double feetToInches;
double inches;
double totalInches;
double weight;
double bmi;
double bmiRoundOff;
weight = Double.parseDouble(weightET.getText().toString());
feet = Double.parseDouble(feetET.getText().toString());
inches = Double.parseDouble(inchesET.getText().toString());
feetToInches = feet * 12;
totalInches = feetToInches + inches;
bmi = (weight * 703) / (totalInches * totalInches);
bmiRoundOff = Math.round(bmi * 100.00) / 100.00;
yourBMI.setText("Your BMI Is " + String.valueOf(bmiRoundOff));
if(bmiRoundOff < 18.5)
{
bmiClass.setText("Your BMI Class: Underweight");
}
if(bmiRoundOff > 18.5 && bmiRoundOff <= 24.9)
{
bmiClass.setText("Your BMI Class: Normal");
}
if(bmiRoundOff >= 25.0 && bmiRoundOff <= 29.0)
{
bmiClass.setText("Your BMI Class: Overweight!");
}
if(bmiRoundOff > 29.0) {
bmiClass.setText("Your BMI Class: Obese");
}
}
public void main (View view)
{
Intent intent = new Intent(getApplicationContext() , MainActivity.class);
startActivity(intent);
}
Here is the main activity:
public void metric (View view)
{
Intent i = new Intent(getApplicationContext() , metric_activity.class);
startActivity(i);
}
public void imperial (View view)
{
Intent i = new Intent(getApplicationContext() , imperial_activity.class);
startActivity(i);
}
Logcat:
Process: com.jdaapps.bmicalculator, PID: 2004
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jdaapps.bmicalculator/com.jdaapps.bmicalculator.metric_activity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.jdaapps.bmicalculator.metric_activity.performCalculation(metric_activity.java:23)
at com.jdaapps.bmicalculator.metric_activity.onCreate(metric_activity.java:110)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)