【问题标题】:OnClickListener error: Source not foundOnClickListener 错误:找不到源
【发布时间】:2010-06-01 23:29:59
【问题描述】:

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

【问题讨论】:

    标签: android


    【解决方案1】:

    定义 TextView Gfrtext...

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

    实际上,您得到的是 NullPointerException,请查看 LogCat 或 Debug 视图以了解有关您的应用异常的更多具体细节。

    这是个大问题!!! =)

    【讨论】:

    • 谢谢,伙计。不幸的是,现在我在项目层次结构级别遇到了这个错误。未知错误:javax.xml.xpath.XPathExpressionException
    【解决方案2】:

    我认为您缺少对 Female/African/EditAge/etc 的初始化。在 onCreate 方法中。在这里,您应该使用 findViewById 方法加载所有这些。这可以在调试时轻松检查(尝试在 onClick 方法的第一行放置断点)。

    顺便说一下,Java 中的约定是对象的成员和方法始终以小写字母开头,而对象名称以大写字母开头。

    【讨论】:

    • 好的,这不是问题所在。但是你试过调试吗?
    【解决方案3】:

    您的代码没有任何问题!那是一个 Eclipse 异常 检查这个... Eclipse debugging “source not found”

    【讨论】:

    • 谢谢,但我看了那个问题,它似乎没有解决同样的问题。或者也许确实如此,但我似乎无法将它用作我的情况的解决方案。我设置了一堆断点,似乎在calculateGFR方法运行后立即发生错误。
    • 从其他帖子看来,我需要下载Android源码....
    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    相关资源
    最近更新 更多