I am trying to get an int from an edit text set to inputType=”number” and then out put it ( i will do something with it later) i cant do it, this is the code i have which i have been told to try but it dose not work
Button go;
EditText num;
TextView OP, ST;
String OPS;
int oer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initalize();
go.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int myNum = 0;
OPS = num.getText().toString();
try {
myNum = Integer.parseInt(OPS);
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
OP.setText(myNum);
}
After i run it it force closes.
This Is the LOG CAT:
09-09 16:53:00.400: W/ResourceType(20145): No package identifier when getting value for resource number 0x00000008
09-09 16:53:00.400: D/AndroidRuntime(20145): Shutting down VM
09-09 16:53:00.400: W/dalvikvm(20145): threadid=1: thread exiting with uncaught exception (group=0x40a411f8)
09-09 16:53:00.561: E/AndroidRuntime(20145): FATAL EXCEPTION: main
09-09 16:53:00.561: E/AndroidRuntime(20145): android.content.res.Resources$NotFoundException: String resource ID #0x8
09-09 16:53:00.561: E/AndroidRuntime(20145): at android.content.res.Resources.getText(Resources.java:248)
09-09 16:53:00.561: E/AndroidRuntime(20145): at android.widget.TextView.setText(TextView.java:3473)
09-09 16:53:00.561: E/AndroidRuntime(20145): at com.Nutty.studios.randnumgen.free.RandomNumberGenActivity$1.onClick(RandomNumberGenActivity.java:42)
09-09 16:53:00.561: E/AndroidRuntime(20145): at android.view.View.performClick(View.java:3511)
09-09 16:53:00.561: E/AndroidRuntime(20145): at android.view.View$PerformClick.run(View.java:14105)
09-09 16:53:00.561: E/AndroidRuntime(20145): at android.os.Handler.handleCallback(Handler.java:605)
09-09 16:53:00.561: E/AndroidRuntime(20145): at android.os.Handler.dispatchMessage(Handler.java:92)
09-09 16:53:00.561: E/AndroidRuntime(20145): at android.os.Looper.loop(Looper.java:137)
09-09 16:53:00.561: E/AndroidRuntime(20145): at android.app.ActivityThread.main(ActivityThread.java:4575)
09-09 16:53:00.561: E/AndroidRuntime(20145): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 16:53:00.561: E/AndroidRuntime(20145): at java.lang.reflect.Method.invoke(Method.java:511)
09-09 16:53:00.561: E/AndroidRuntime(20145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
09-09 16:53:00.561: E/AndroidRuntime(20145): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
09-09 16:53:00.561: E/AndroidRuntime(20145): at dalvik.system.NativeStart.main(Native Method)
it works fine if i try to out put the string but not as an integer?
1. You will never get an
intfrom EditText,InputTypeis provided to provide you with the correct softKeyboard…2. You can convert the text from EditText in to integer using this..
int i = Integer.parseInt(editText.getText().toString());moreover
setTextwill expect you to provide it with aString…. so you need to do this…OP.setText(myNum+"");toString()only works withObjecttypes, and as you have declaredmyNumasint, which is a primitive type…. you need to use+ ""