Im very new to java (mainly duel with embedded only)
Im writing a simple android program that passes a value of editText input to another editText by a click of a button. However every-time I hit the button my program crashes. would like anybody comments 🙂
This is my main:
package Mushroom.Pickers.Note;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
//import android.widget.Button;
//import android.widget.TableLayout;
public class Mushy extends Activity {
//private TableLayout layout;
//private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void Compute_data(View view) {//Function to perform on click
//Perform calculation
int Hours;
EditText start_time = (EditText)findViewById(R.id.editText26) ;
Hours = Integer.parseInt(start_time.getText().toString());
EditText test_time = (EditText)findViewById(R.id.editText43);
test_time.setText(Hours);
}
}
and in my xml:
<Button
android:id="@+id/Button1"
android:layout_width="134px"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textSize="15sp"
android:text="Calculate"
android:onClick="Compute_data"
android:textColor="#1e90ff" />
</TableRow>
<EditText
android:id="@+id/editText26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:inputType="number"
android:width="350px" />
<EditText
android:id="@+id/editText43"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:inputType="number"
android:width="350px" />
The layout appears fine, just the matter of crashing when the button is clicked 🙁
P/S: here is the logcat:
12-19 03:03:02.106: W/ResourceType(667): No package identifier when getting value for resource number 0x00000004
12-19 03:03:02.106: D/AndroidRuntime(667): Shutting down VM
12-19 03:03:02.106: W/dalvikvm(667): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-19 03:03:02.275: E/AndroidRuntime(667): FATAL EXCEPTION: main
12-19 03:03:02.275: E/AndroidRuntime(667): java.lang.IllegalStateException: Could not execute method of the activity
12-19 03:03:02.275: E/AndroidRuntime(667): at android.view.View$1.onClick(View.java:3039)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.view.View.performClick(View.java:3480)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.view.View$PerformClick.run(View.java:13983)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.os.Handler.handleCallback(Handler.java:605)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.os.Looper.loop(Looper.java:137)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-19 03:03:02.275: E/AndroidRuntime(667): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 03:03:02.275: E/AndroidRuntime(667): at java.lang.reflect.Method.invoke(Method.java:511)
12-19 03:03:02.275: E/AndroidRuntime(667): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-19 03:03:02.275: E/AndroidRuntime(667): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-19 03:03:02.275: E/AndroidRuntime(667): at dalvik.system.NativeStart.main(Native Method)
12-19 03:03:02.275: E/AndroidRuntime(667): Caused by: java.lang.reflect.InvocationTargetException
12-19 03:03:02.275: E/AndroidRuntime(667): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 03:03:02.275: E/AndroidRuntime(667): at java.lang.reflect.Method.invoke(Method.java:511)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.view.View$1.onClick(View.java:3034)
12-19 03:03:02.275: E/AndroidRuntime(667): ... 11 more
12-19 03:03:02.275: E/AndroidRuntime(667): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x4
12-19 03:03:02.275: E/AndroidRuntime(667): at android.content.res.Resources.getText(Resources.java:247)
12-19 03:03:02.275: E/AndroidRuntime(667): at android.widget.TextView.setText(TextView.java:3428)
12-19 03:03:02.275: E/AndroidRuntime(667): at Mushroom.Pickers.Note.Mushy.Compute_data(Mushy.java:29)
12-19 03:03:02.275: E/AndroidRuntime(667): ... 14 more
12-19 03:03:02.475: D/dalvikvm(667): GC_CONCURRENT freed 230K, 4% free 10426K/10759K, paused 6ms+14ms
Problem:
test_time.setText(value)have overridden methods of bothintandStringparameters.intparameter indicate that you will passStringresource id. (i.eR.string.any_string)
Stringparameter indicate that you will passString.you were passing Hours as int not as String.
Solution:
you will convet int to String to set as text of any View (i.e
Button,TextView,EditText)Modify your code as below: