0

I am trying to make a simple app that takes to values entered in EditTexts, calculate the result and set the result in the TextView. The action is handled by OnClick for the button in XML layout file. The app starts, but when the button is clicked - it crashes. What am I doing wrong? Here's my method that is handled by OnClick.

public void calculate() {

    EditText wapnIn = (EditText) findViewById(R.id.wapn_ozn);
    EditText albIn = (EditText) findViewById(R.id.alb_ozn);
    TextView wynikOut = (TextView) findViewById(R.id.wynik_wapn_alb);

    double wapn = 0;
    double alb = 0;
    double wynik = 0;

    wapn = Double.parseDouble(wapnIn.getText().toString());
    alb = Double.parseDouble(albIn.getText().toString());

    wynik = ((40 - alb) * 0.02 + wapn);

    wynikOut.setText(String.valueOf(wynik));

}

Any suggestions are much appreciated. I looked in other questions, but I haven't found answer to what am I doing wrong here and how to fix it.

EDIT: As Krauxe suggested, here's the stack trace:

05-18 04:55:49.960  16888-16888/pl.roykovsky.doctorassistant   E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.IllegalStateException: Could not find a method calculate(View) in the activity class pl.roykovsky.doctorassistant.MainActivity for onClick handler on view class android.widget.Button with id 'oblicz_wapn_alb'
        at android.view.View$1.onClick(View.java:3031)
        at android.view.View.performClick(View.java:3511)
        at android.view.View$PerformClick.run(View.java:14110)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NoSuchMethodException: calculate [class android.view.View]
        at java.lang.Class.getConstructorOrMethod(Class.java:460)
        at java.lang.Class.getMethod(Class.java:915)
        at android.view.View$1.onClick(View.java:3024)
        ... 11 more

Frankly, it's the first time I looked at stack trace (I'm a beginner), and seems like it's a problem with the method. I checked, and it's the same method's name in XML and in MainActivity.

4

2 回答 2

2
public void calculate( View v) {

define function as above

于 2013-05-18T02:59:52.627 回答
2

The problem is when OnClick is triggered, it passes a View when calling calculate(), yet you defined your calculate method without any arguments.

You need to modify your method to:

public void calculate(View v) { // Make it accept a View

    EditText wapnIn = (EditText) findViewById(R.id.wapn_ozn);
    EditText albIn = (EditText) findViewById(R.id.alb_ozn);
    TextView wynikOut = (TextView) findViewById(R.id.wynik_wapn_alb);

    double wapn = 0;
    double alb = 0;
    double wynik = 0;

    wapn = Double.parseDouble(wapnIn.getText().toString());
    alb = Double.parseDouble(albIn.getText().toString());

    wynik = ((40 - alb) * 0.02 + wapn);

    wynikOut.setText(String.valueOf(wynik));

}
于 2013-05-18T03:02:00.080 回答