Reputation: 1734
I have 2 Edittext in my program and a button. I want to check if the contents of the edittexts matches a pattern, it it does perform some calculation. So onClick on the the button an created it corresponding method in my Java file but i keep getting errors in my log cat. Here is my onClick method.
public void multiply(){
final EditText start = (EditText) findViewById(R.id.editText_InputStart);
final EditText stop = (EditText) findViewById(R.id.editText_InputStop);
if(start.getText().toString().length() == 0){
start.setError("Enter at least a Number");
if(!start.getText().toString().matches("[0-9]")){
start.setError("Enter a Number");
if (stop.getText().toString().length() == 0){
stop.setError("Enter at least a Number");
if (!stop.getText().toString().matches("[0-9]")){
stop.setError("Enter a Number");
}
}
}
} else{
calculate(start.getText().toString(), stop.getText().toString());
}
}
Here is the log cat errors.
02-28 06:38:52.308: E/AndroidRuntime(1125): FATAL EXCEPTION: main
02-28 06:38:52.308: E/AndroidRuntime(1125): Process: com.wecanIT.multiplicationtables, PID: 1125
02-28 06:38:52.308: E/AndroidRuntime(1125): java.lang.IllegalStateException: Could not find a method multiply(View) in the activity class com.wecanIT.multiplicationtables.MultiplyMainActivity for onClick handler on view class android.widget.Button with id 'process'
02-28 06:38:52.308: E/AndroidRuntime(1125): at android.view.View$1.onClick(View.java:3810)
02-28 06:38:52.308: E/AndroidRuntime(1125): at android.view.View.performClick(View.java:4438)
02-28 06:38:52.308: E/AndroidRuntime(1125): at android.view.View$PerformClick.run(View.java:18422)
02-28 06:38:52.308: E/AndroidRuntime(1125): at android.os.Handler.handleCallback(Handler.java:733)
02-28 06:38:52.308: E/AndroidRuntime(1125): at android.os.Handler.dispatchMessage(Handler.java:95)
02-28 06:38:52.308: E/AndroidRuntime(1125): at android.os.Looper.loop(Looper.java:136)
02-28 06:38:52.308: E/AndroidRuntime(1125): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-28 06:38:52.308: E/AndroidRuntime(1125): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 06:38:52.308: E/AndroidRuntime(1125): at java.lang.reflect.Method.invoke(Method.java:515)
02-28 06:38:52.308: E/AndroidRuntime(1125): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-28 06:38:52.308: E/AndroidRuntime(1125): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-28 06:38:52.308: E/AndroidRuntime(1125): at dalvik.system.NativeStart.main(Native Method)
02-28 06:38:52.308: E/AndroidRuntime(1125): Caused by: java.lang.NoSuchMethodException: multiply [class android.view.View]
02-28 06:38:52.308: E/AndroidRuntime(1125): at java.lang.Class.getConstructorOrMethod(Class.java:472)
02-28 06:38:52.308: E/AndroidRuntime(1125): at java.lang.Class.getMethod(Class.java:857)
02-28 06:38:52.308: E/AndroidRuntime(1125): at android.view.View$1.onClick(View.java:3803)
02-28 06:38:52.308: E/AndroidRuntime(1125): ... 11 more
Found the problem in my Log Cat my method Multiply should accept a parameter View thanks to all.
Thanks.
Upvotes: 0
Views: 171
Reputation: 1734
The problem was simply with the onClick method for the button. It is meant to receive a View parameter which i forgot to pass in. So the code should be :
public void multiply(View v){
final EditText start = (EditText) findViewById(R.id.editText_InputStart);
final EditText stop = (EditText) findViewById(R.id.editText_InputStop);
String regEx = "[0-9]{1,3}";
String str = start.getText().toString();
String str1 = stop.getText().toString();
if (str.length() != 0 && str.matches(regEx) && str1.length() != 0 && str1.matches(regEx)){
calculate(str,str1);
} else if(str.length() == 0 || !str.matches(regEx)) {
start.setError("Enter a Number");
} else if (str1.length() == 0 || !str1.matches(regEx)){
stop.setError("Enter a Number");
}
}
Upvotes: 0
Reputation: 1074
first put define inputType for EditText in xml file.
android:inputType="number"
and check like this :
String errorMsg = "";
String str = edittext.getText().toString();
if (checkInputAvailable(str)){
//do your work
}else{
Toast.makeText(context, ""+errorMsg, Toast.LENGTH_SHORT).show();
}
private boolean checkInputAvailable(String str) {
boolean retVal = true;
if (str.length() == 0) {
errorMsg = "Enter at least a Number";
retVal = false;
} else if (!TextUtils.isDigitsOnly(str)) {
errorMsg = "Enter a Number";
retVal = false;
}
return retVal;
}
Upvotes: 3