OnCheckedChangeListener & OnClickListener in android -- Button half-works on first click, finishes on second click?

Tony Stevens picture Tony Stevens · Jul 5, 2012 · Viewed 9.1k times · Source

Well... here's a problem it looks like I'm not the first to experience looking through other questions, however I can't find one that's Android-specific (others are C++ or straight java with different scenarios).

I have a calculator that determines your fuel mileage needs with given user inputs. The thing I'm adding now is a "burnoff" aspect where you can calculate the weight lost over the course of the race. Before adding the weight/burnoff element, everything worked fine.

Now, everything calculates normally except the weight on the first click.

On the second click (and subsequent clicks) it calculates properly. I suspect it has something to do with the switch statement and its location, but I could be wrong.... and even if I'm not, I'm not sure how to change it.

Looking for help on getting it all functioning properly on the first click. Thanks!

EDIT: The non-exception Toast text I put in as a debugger to determine if it was a math issue or what in the code. It pops up with "0.0" on the first click, then either "6.2" or "6.6" on subsequent clicks.


    package com.tomcat.performance;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    import android.widget.TextView;
    import android.widget.Toast;


    public class Fuelsimple extends Activity implements OnCheckedChangeListener{

double fuelweight;

public void onCheckedChanged(RadioGroup group, int checkedId) {
    // TODO Auto-generated method stub
    switch (checkedId){
    case R.id.rbMethanol:
        fuelweight = 6.6;
        break;
    case R.id.rbGasoline:
        fuelweight = 6.2;
        break;}
}

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

RadioGroup rgFuelType = ((RadioGroup) findViewById (R.id.rgFuelType));
rgFuelType.setOnCheckedChangeListener(this);
RadioButton rbMethanol = ((RadioButton) findViewById(R.id.rbMethanol));
RadioButton rbGasoline = ((RadioButton) findViewById(R.id.rbGasoline));

Button gen = ((Button) findViewById(R.id.submit));  
gen.setOnClickListener(new View.OnClickListener() { 



        public void onClick(View v) {
    // TODO Auto-generated method stub      

            EditText fuelUsed, pracLaps, featureLaps;

            pracLaps = ((EditText) findViewById(R.id.pracLaps));
            fuelUsed = ((EditText) findViewById(R.id.fuelUsed));
            featureLaps = ((EditText) findViewById(R.id.featureLaps));
            TextView textLPGValue, textFuelNeededValue, textBurnoffValue;
            try{
            double pracLapsVar = Double.parseDouble(pracLaps.getText().toString());
            double fuelUsedVar = Double.parseDouble(fuelUsed.getText().toString());
            double featureLapsVar = Double.parseDouble(featureLaps.getText().toString());

            double efficiency = (pracLapsVar / fuelUsedVar);
            double fuelNeeded = (featureLapsVar / efficiency);
            double burnoff = (1.05 * (fuelNeeded * fuelweight));

            Toast andJelly = Toast.makeText(Fuelsimple.this, String.valueOf(fuelweight), Toast.LENGTH_LONG);
            andJelly.show();


            textLPGValue = ((TextView) findViewById(R.id.textLPGValue));
            textFuelNeededValue = ((TextView) findViewById(R.id.textFuelNeededValue));
            textBurnoffValue = ((TextView) findViewById(R.id.textBurnoffValue));

            textLPGValue.setText(String.valueOf(String.format("%.3f", efficiency)) + " laps per gallon");
            textFuelNeededValue.setText(String.valueOf(String.format("%.3f", fuelNeeded)) + " gallons");
            textBurnoffValue.setText(String.valueOf(String.format("%.2f", burnoff)) + " pounds");
            } catch (NumberFormatException e) {Toast andEggs = Toast.makeText(Fuelsimple.this, "Please complete all fields and enter your fuel & lap info in decimals or whole numbers.", Toast.LENGTH_LONG); andEggs.show();} 
            catch (NullPointerException n) {Toast andEggs = Toast.makeText(Fuelsimple.this, "Please enter ALL lap and fuel data.", Toast.LENGTH_LONG);
            andEggs.show();}
        }}
);      
}
    public void onClick(View v) {
    }
    }

Answer

MattDavis picture MattDavis · Jul 5, 2012

As far as I can tell, your variable fuelWeight is never initialized until a radio button is pressed. The simplest way to fix this would be to call setChecked(true) on either rbmethanol or rbGasoline after setting the listener, maybe right below where you initialize those variables. Doing it after setting the listener is important, because you want the switch statement to be resolved. Right now, before you press a radio button, fuelWeight, as with all numeric primitive data types, will be initialized to 0. This is probably the reason your first calculations are wrong.