将变量值传递给TextView

时间:2014-08-09 09:42:47

标签: android

我是这个世界的新手,我刚学会了如何在objective-c中做一些简单的代码,但现在我正在努力学习甚至Java和Android的基础。所以..即使对于我使用Obj-C,这也很简单,但我遇到了麻烦。

我想显示int" numCas"的值。进入TextView所以我喜欢这样:

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);
}

按下按钮我可以在控制台中看到随机数的日志,我可以看到文字" Hello"在屏幕上...但如果我交换评论:

   // scrittaNumero.setText("Hello!");
    scrittaNumero.setText(numCas);

我收到错误!我知道这是一个菜鸟问题,但我没有在任何教程中找到方法!对不起,谢谢!

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)

3 个答案:

答案 0 :(得分:1)

尝试:scrittaNumero.setText(String.valueOf( numCas ) ); 您必须将numCas的值强制转换为String,因为setText只接受String。

答案 1 :(得分:1)

更改此

scrittaNumero.setText(numCas);

scrittaNumero.setText(Integer.toString(numCas));

您将int值传递给setText,因此它会引用ids中定义的xml。适当的方法是将该int转换为String并将其传递给setText()

答案 2 :(得分:0)

您需要将其强制转换为String(现在编译器不会将其解释为资源):

TextView.setText(numCas + "").
TextView.setText(String.valueOf(numCas));
TextView.setText(Integer.toString(numCas));