I am new at this world, and I just learned how to do some simple code in objective-c but now I am trying to learn basis even for Java and Android. So .. This would be simple even for me with Obj-C but I am having troubles with Java.
I'd want to show the value of the int "numCas" into a TextView so I did like this:
public void casuale (View v) {
int numCas = (int) ((Math.random()*10)+1);
System.out.println("Numero: "+ numCas);
TextView scrittaNumero;
scrittaNumero = (TextView)findViewById(R.id.textView2);
scrittaNumero.setText("Hello!");
// scrittaNumero.setText(numCas);
}
pressing a button I can see the log of a random number in the console and I can see the text "Hello" on screen ... but If I Swap the comments:
// scrittaNumero.setText("Hello!");
scrittaNumero.setText(numCas);
I Get errors! I know this is a noob question but I did not find a way in any tutorial! Sorry and thanks in advance!
08-09 05:06:53.017 7472-7472/com.ciavapps.com.onclicktest2 I/System.out﹕ Numero: 7
08-09 05:06:53.017 7472-7472/com.ciavapps.com.onclicktest2 W/ResourceType﹕ No package identifier when getting value for resource number 0x00000007
08-09 05:06:53.057 7472-7472/com.ciavapps.com.onclicktest2 D/AndroidRuntime﹕ Shutting down VM
08-09 05:06:53.057 7472-7472/com.ciavapps.com.onclicktest2 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2ae5ba8)
08-09 05:06:53.077 7472-7472/com.ciavapps.com.onclicktest2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ciavapps.com.onclicktest2, PID: 7472
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7
at android.content.res.Resources.getText(Resources.java:244)
at android.widget.TextView.setText(TextView.java:3888)
at com.ciavapps.com.onclicktest2.MyActivity.casuale(MyActivity.java:60)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Try: scrittaNumero.setText(String.valueOf( numCas ) );
You have to cast the value of numCas to a String, because setText accepts only String.
Change this
scrittaNumero.setText(numCas);
to
scrittaNumero.setText(Integer.toString(numCas));
You're passing an int value to setText, so it will refer to ids defined in xml. Proper way for you is to convert that int to String and pass it to setText()
You need to cast it to String (now compiler won't it interpret as resource):
TextView.setText(numCas + "").
TextView.setText(String.valueOf(numCas));
TextView.setText(Integer.toString(numCas));
User contributions licensed under CC BY-SA 3.0