下面是我使用的 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