简体   繁体   中英

Clicking outside a textBox or button force closes my app with IllegalStateException

Below is the Java file that I used. All of the code works really well, but the error above still lingers. Can anyone see the problem?

package tip.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TipCalculator extends Activity 
{
    private Button enter;
    EditText myEditField;
    EditText myEditField2;
    float percentage = 0;
    float percentageInp = 0;
    float billAmount = 0;
    double output = 0; 
    String output1 = "";
    Button clearButton ;
    TextView textView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myEditField = (EditText) findViewById(R.id.percentText);
        enter = (Button)findViewById(R.id.button1);
        myEditField2 = (EditText) findViewById(R.id.billText);
        clearButton = (Button) findViewById(R.id.clearButton);

        enter.setOnClickListener( new OnClickListener() {
            public void onClick(View v) {
                TextView errors;
                textView = (TextView) findViewById(R.id.textView1);
                errors = (TextView) findViewById(R.id.textView4); 
                int a = 0;
                if(myEditField2.getText().toString().equals("")) {
                    errors.setText("Error");
                } else {
                    a++;
                }

                if(myEditField.getText().toString().equals("")){
                    errors.setText("Error");
                } else {
                    a++;
                }

                if(a == 2) {
                    percentageInp = Float.parseFloat(myEditField.getText().toString());
                    billAmount = Float.parseFloat(myEditField2.getText().toString());
                    percentage = ((float)percentageInp /100);

                    output = (double)(billAmount * percentage);

                    double result = output * 100;
                    result = Math.round(result);
                    result = result / 100;

                    output1 = Double.toString(result);

                    View.setText(output1 + " $");
                    errors.setText("");
                }
            }
        });

        clearButton.setOnClickListener( new OnClickListener() {
            public void onClick(View v) {
                TextView errors;
                textView = (TextView) findViewById(R.id.textView1);
                errors = (TextView) findViewById(R.id.textView4); 
                int a = 0;

                if (myEditField2.getText().toString().equals("")) {
                    myEditField.setText("");
                    percentage = 0;
                    percentageInp = 0;
                    billAmount = 0;
                    output = 0; 
                    textView.setText("");
                    output1 = "";
                } else {
                    a++;
                }

                if(myEditField.getText().toString().equals("")) {
                    myEditField2.setText("");
                    percentage = 0;
                    percentageInp = 0;
                    billAmount = 0;
                    output = 0; 
                    output1 = "";
                    textView.setText("");
                 } else {
                    a++;
                 }

                 if (a == 2) {
                    myEditField.setText("");
                    myEditField2.setText("");
                    percentage = 0;
                    percentageInp = 0;
                    billAmount = 0;
                    output = 0; 
                    output1 = "";

                    textView.setText("");
                    errors.setText("");
                }
            }
        });
    }
}

Here is the logcat, where I get an error I don't understand at all. I never call the method onEnterClick() ?

08-02 21:17:37.205: E/AndroidRuntime(27185): FATAL EXCEPTION: main
08-02 21:17:37.205: E/AndroidRuntime(27185): java.lang.IllegalStateException: Could not find a method onEnterClick(View) in the activity class tip.calculator.TipCalculator for onClick handler on view class android.widget.RelativeLayout with id 'errorText'
08-02 21:17:37.205: E/AndroidRuntime(27185):    at android.view.View$1.onClick(View.java:3085)
08-02 21:17:37.205: E/AndroidRuntime(27185):    at android.view.View.performClick(View.java:3620)
08-02 21:17:37.205: E/AndroidRuntime(27185):    at android.view.View$PerformClick.run(View.java:14292)
08-02 21:17:37.205: E/AndroidRuntime(27185):    at android.os.Handler.handleCallback(Handler.java:605)
08-02 21:17:37.205: E/AndroidRuntime(27185):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-02 21:17:37.205: E/AndroidRuntime(27185):    at android.os.Looper.loop(Looper.java:137)
08-02 21:17:37.205: E/AndroidRuntime(27185):    at android.app.ActivityThread.main(ActivityThread.java:4507)
08-02 21:17:37.205: E/AndroidRuntime(27185):    at java.lang.reflect.Method.invokeNative(Native Method)
08-02 21:17:37.205: E/AndroidRuntime(27185):    at java.lang.reflect.Method.invoke(Method.java:511)
08-02 21:17:37.205: E/AndroidRuntime(27185):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-02 21:17:37.205: E/AndroidRuntime(27185):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-02 21:17:37.205: E/AndroidRuntime(27185):    at dalvik.system.NativeStart.main(Native Method)
08-02 21:17:37.205: E/AndroidRuntime(27185): Caused by: java.lang.NoSuchMethodException: onEnterClick [class android.view.View]
08-02 21:17:37.205: E/AndroidRuntime(27185):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
08-02 21:17:37.205: E/AndroidRuntime(27185):    at java.lang.Class.getMethod(Class.java:915)
08-02 21:17:37.205: E/AndroidRuntime(27185):    at android.view.View$1.onClick(View.java:3078)
08-02 21:17:37.205: E/AndroidRuntime(27185):    ... 11 more

I think you have defined the onclick method in the XML Layout also. And you have written the onclick listener in the program. Remove the following line from your xml layout and your program works fine:

android:onclick="onEnterClick"

This because of you're setting android:onClick="onEnterClick" to your RelativeLayout And, you didn't implement this in your TipCalculator.java file. So, there you've an exception with IllegalStateException See your LogCat 's first line -

Could not find a method onEnterClick(View) in the activity class tip.calculator.TipCalculator for onClick handler on view class android.widget.RelativeLayout with id 'errorText'

You can easily understand with above line of exception.

Maybe its because you have declared a function onEnterClick in your Button layout in xml . There will be a line like android:onClick="onEnterClick in your button layout.

So define the action you want when you press that button, inside

public void onEnterClick(View view) 

in your java class.

note: you first have to create this function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM