Reputation: 1
I'm a new programmer, and I only know the basics of Java/Android programming. I'm trying to make a basic app to convert Fahrenheit to Celsius. I coded the method to do it, hooked it up to a button, and my app crashes. I could not seem to find the error and/or an alternative way of doing it. I was wondering if anyone knew what caused the crash and if they can tell me how to avoid it in the future. This would benefit me as well as beginner Java programmers.
Here is my Java Code:
public void appControl (View view){
EditText giveFarenheit = (EditText) findViewById(R.id.farenheit_int);
String oldFarenheitString = giveFarenheit.getText().toString();
int oldFarenheit=Integer.parseInt(oldFarenheitString);
int step1 = oldFarenheit - 32;
int newCelcius = step1*5/9;
EditText giveCelcius = (EditText) findViewById(R.id.celcius_int);
giveCelcius.setHint(oldFarenheit);
}
Here is the log with the crash:
08-28 22:07:55.384 14825-14825/com.paulrotman.tempconverterfree E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.paulrotman.tempconverterfree, PID: 14825
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4247)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4242)
at android.view.View.performClick(View.java:5191)
Upvotes: 0
Views: 68
Reputation: 668
Change this.
(EditText) view.findViewById(R.id.farenheit_int);
public void appControl (View view){
EditText giveFarenheit = (EditText) view.findViewById(R.id.farenheit_int);
String oldFarenheitString = giveFarenheit.getText().toString();
int oldFarenheit=Integer.parseInt(oldFarenheitString);
int step1 = oldFarenheit - 32;
int newCelcius = step1*5/9;
EditText giveCelcius = (EditText) view.findViewById(R.id.celcius_int);
giveCelcius.setHint(String.ValueOf(oldFarenheit));
}
Upvotes: 1
Reputation: 25267
Your this line is causing error, i guess:
giveCelcius.setHint(oldFarenheit);
Its simply because you are giving integer as an argument instead of CharacterSequence[]
(I guess); So just add +""
to make it String, and your code should work.
...
giveCelcius.setHint(oldFarenheit+"");
Upvotes: 2
Reputation: 79
don't initialize EditText giveFarenheit inside a method. instead of doing this way
do like this EditText giveFarenheit; above the method
giveFarenheit = (EditText) findViewById(R.id.farenheit_int); it may be resolve the problem
see the bellow link Android java.lang.IllegalStateException: Could not execute method of the activity
Upvotes: 0