开发者

OnClickListener error: Source not found

开发者 https://www.devze.com 2023-01-02 02:48 出处:网络
I\'m brand new to Android development and right now I am building a simple calculator for healthcare workers. My program implements the OnClickListener class, but every time I click on the button to i

I'm brand new to Android development and right now I am building a simple calculator for healthcare workers. My program implements the OnClickListener class, but every time I click on the button to initiate the calculation, I get an error saying the "Source is not Found".

Here is the code:

public class KidneyeGFR extends Activity implements OnClickListener {
TextView EditAge;
TextView EditSerum;
TextView Gfrtext;
RadioButton Male;
RadioButton Female;
RadioButton EveryoneElse;
RadioButton African;
Button Calculate;
double gender;
double race;
double finalgfr;
private static final int GFRCONST = 186;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    EditAge = (TextView)this.findViewById(R.id.EditAge);
    EditSerum = (TextView)this.findViewById(R.id.EditSerum);

    Male = (RadioButton)this.findViewById(R.id.Male);
    Male.setChecked(true);
    Female = (RadioButton)this.findViewById(R.id.Female);

    EveryoneElse = (RadioButton)this.findViewById(R.id.EveryoneElse);
    EveryoneElse.setChecked(true);
    African = (RadioButton)this.findViewById(R.id.African);

    Calculate = (Button)this.findViewById(R.id.Calculate);
    Calculate.setOnClickListener(this);

}

public void onClick(View v) {
    if (Female.isChecked()) {
        gender = 0.742;
    }
    else {
        gender = 1.0;
    }
    if (African.isChecked()) {
        race = 1.212;
    }
    else {
        race = 1.0;
    }
    calculateGF开发者_Go百科R();
}


protected void calculateGFR() {
    int age = Integer.parseInt(EditAge.getText().toString());
    double serum = Double.parseDouble(EditSerum.getText().toString());
    finalgfr = GFRCONST * Math.pow(serum, -1.154) * Math.pow(age, -0.203) * gender * race;
    Gfrtext.setText(Double.toString(finalgfr));
}


define the TextView Gfrtext...

  Gfrtext = (TextView)this.findViewById(R.id.Gfrtext);

Actually you are getting a NullPointerException, check the LogCat or Debug view to have more specific details about your app exceptions.

Thats the big problem!!! =)


I think that you are missing the initialization of Female/African/EditAge/etc. in the onCreate method. Here you should load all of these using the findViewById method. This can easily be checked when debugging (try placing a breakpoint on the first line of the onClick method).

By the way, the convention in Java is that members and methods of an object always start with a lower case and that object names start with an upper case.


Your code doesn't have any trouble ! thats an Eclipse Exception check this... Eclipse debugging “source not found”

0

精彩评论

暂无评论...
验证码 换一张
取 消