0

下面是我使用的 Java 文件。所有代码都运行良好,但上面的错误仍然存​​在。任何人都可以看到问题吗?

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

这是logcat,我得到一个我根本不明白的错误。我从不调用方法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
4

3 回答 3

3

我认为您也在 XML 布局中定义了 onclick 方法。并且您已经在程序中编写了 onclick 侦听器。从您的 xml 布局中删除以下行,您的程序可以正常工作:

android:onclick="onEnterClick"
于 2012-08-03T04:34:47.270 回答
1

这是因为您设置android:onClick="onEnterClick"RelativeLayout并且,您没有在TipCalculator.java文件中实现此功能。所以,你有一个例外,IllegalStateException看你LogCat的第一行 -

在 id 为“errorText”的视图类 android.widget.RelativeLayout 上的 onClick 处理程序的活动类 tip.calculator.TipCalculator 中找不到方法 onEnterClick(View)

你可以很容易地理解上面的异常行。

于 2012-08-03T04:34:22.230 回答
0

也许是因为您在 xml 的 Button 布局中声明了一个函数 onEnterClick 。android:onClick="onEnterClick在您的按钮布局中会有一条线。

所以当你按下那个按钮时,定义你想要的动作,里面

public void onEnterClick(View view) 

在你的java类中。

注意:您首先必须创建此功能。

于 2012-08-03T07:16:56.413 回答