2012-01-08 8 views
13

私はうまくいけばそうのように、整数の値としてTextViewを設定したい:私はこのエラーを取得し、このANを試してみましたテキストの設定

tv.setText(int) 

。値が別のクラスの中にある場合

はまた、私の整数値は、別のクラス一般

D/AndroidRuntime: Shutting down VM 
E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.application, PID: 29603 
    java.lang.IllegalStateException: Could not execute method for android:onClick 
     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 
     at android.view.View.performClick(View.java:5207) 
     at android.view.View$PerformClick.run(View.java:21168) 
     at android.os.Handler.handleCallback(Handler.java:746) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5443) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
    Caused by: java.lang.reflect.InvocationTargetException 
     at java.lang.reflect.Method.invoke(Native Method) 
     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
     at android.view.View.performClick(View.java:5207) 
     at android.view.View$PerformClick.run(View.java:21168) 
     at android.os.Handler.handleCallback(Handler.java:746) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5443) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
    Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1 
     at android.content.res.Resources.getText(Resources.java:312) 
     at android.widget.TextView.setText(TextView.java:4427) 

答えて

27

tv.setText(String.valueOf(int)); 

のですか?あなたはintにのTextViewを設定した場合、それは意志、

しかし:あなたは、他の1からアクセスできるように

public int getValue() { 
    return value; 
} 

:あなたは、そのクラスに必要な値のgetterを作ることができますAndroid リソースIDと解釈されます。 intの値をテキストとして(そしてそれが指しているリソースではなく)欲しい場合は、まずそれをStringにします。

tv.setText(String.valueOf(theOtherClassInstance.getValue())); 

EDITあなたのintは以下のコメントどおりfirstResultであれば、ゲッターは次のようになります。

public int getFirstResult() { 
    return firstResult; 
} 
+0

はので、この場合には、整数は "firstResult" で、私は公共のint型のgetValue(」設定します){"to public int getValue(firstResult){:? – MiKenning

+0

いいえ、getValue()に情報を渡す必要はありません.1つの整数と1つの整数だけを返します。だからあなたはgetValue()を必要とします:) – Eilidh

+0

どのように私の整数の値を知っていますか? – MiKenning

21

を使用すると、XMLファイルからアプリケーションリソースへの参照のうえいるのsetText(int)を行います、値自体ではありません。

整数propertly以下かを設定するには:

tv.setText(""+integer); 

またはよりよい解決策:

tv.setText(String.valueOf(integer)); 
関連する問題