簡體   English   中英

OnClickListener錯誤:找不到源

[英]OnClickListener error: Source not found

我是Android開發的新手,現在我正在為醫護人員構建一個簡單的計算器。 我的程序實現了OnClickListener類,但是每次我單擊按鈕啟動計算時,都會收到一條錯誤消息,提示“找不到源”。

這是代碼:

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;
    }
    calculateGFR();
}


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

定義TextView Gfrtext ...

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

實際上,您正在獲取NullPointerException,請檢查LogCat或Debug視圖以獲取有關您的應用程序異常的更多詳細信息。

那就是大問題!!! =)

我認為您缺少Female / African / EditAge / etc的初始化。 在onCreate方法中。 在這里,您應該使用findViewById方法加載所有這些。 調試時可以很容易地檢查到這一點(嘗試在onClick方法的第一行上放置一個斷點)。

順便說一句,Java中的約定是,對象的成員和方法始終以小寫字母開頭,而對象名稱則以大寫字母開頭。

您的代碼沒有任何麻煩! 多數民眾贊成在Eclipse異常檢查此... Eclipse調試“找不到源”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM