setText无法在TextView中显示一个数字作为文本
我有4个button和1个TextView。 我希望每个button在TextView中添加或减去与值(分析为int)不同的值。 当我点击其中一个button时,会引发致命错误(如下所示)。 我search了一些其他的Stackoverflow讨论,似乎没有任何工作。 这是我目前的活动:
public class PaydownSnowball extends Activity { int aAmt; String debtExtraPayment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.snowball); final Button plusTen = (Button) findViewById(R.id.buttonp10); final Button plusFive = (Button) findViewById(R.id.buttonp5); final Button minusFive = (Button) findViewById(R.id.buttonm5); final Button minusTen = (Button) findViewById(R.id.buttonm10); SharedPreferences sharedPref= getSharedPreferences("chaosdatasnowball", 0); debtExtraPayment = sharedPref.getString("debtExtraPayment", null); debtExtraPayment ="0"; TextView addAmount = (TextView) findViewById(R.id.dispAddAmt); addAmount.setText(debtExtraPayment); aAmt = Integer.parseInt(debtExtraPayment); plusTen.setOnClickListener(new OnClickListener() { public void onClick(View v) { aAmt = aAmt + 10; TextView addAmount = (TextView) findViewById(R.id.dispAddAmt); addAmount.setText(aAmt); } }); plusFive.setOnClickListener(new OnClickListener() { public void onClick(View v) { aAmt = aAmt + 5; TextView addAmount = (TextView) findViewById(R.id.dispAddAmt); addAmount.setText(aAmt); } }); minusTen.setOnClickListener(new OnClickListener() { public void onClick(View v) { aAmt = aAmt - 10; TextView addAmount = (TextView) findViewById(R.id.dispAddAmt); addAmount.setText(aAmt); } }); minusFive.setOnClickListener(new OnClickListener() { public void onClick(View v) { aAmt = aAmt - 5; TextView addAmount = (TextView) findViewById(R.id.dispAddAmt); addAmount.setText(aAmt); } }); debtExtraPayment = String.valueOf(aAmt); addAmount.setText(debtExtraPayment); SharedPreferences.Editor editor= sharedPref.edit(); editor.putString("debtExtraPayment", debtExtraPayment).commit(); }
这里是点击plusFivebutton时发生的logcats:
11-09 15:15:57.907: E/AndroidRuntime(695): FATAL EXCEPTION: main 11-09 15:15:57.907: E/AndroidRuntime(695): android.content.res.Resources$NotFoundException: String resource ID #0x5 11-09 15:15:57.907: E/AndroidRuntime(695): at android.content.res.Resources.getText(Resources.java:201) 11-09 15:15:57.907: E/AndroidRuntime(695): at android.widget.TextView.setText(TextView.java:2817) 11-09 15:15:57.907: E/AndroidRuntime(695): at biz.midl.debtsnowball.PaydownSnowball$2.onClick(PaydownSnowball.java:47) 11-09 15:15:57.907: E/AndroidRuntime(695): at android.view.View.performClick(View.java:2408) 11-09 15:15:57.907: E/AndroidRuntime(695): at android.view.View$PerformClick.run(View.java:8816) 11-09 15:15:57.907: E/AndroidRuntime(695): at android.os.Handler.handleCallback(Handler.java:587) 11-09 15:15:57.907: E/AndroidRuntime(695): at android.os.Handler.dispatchMessage(Handler.java:92) 11-09 15:15:57.907: E/AndroidRuntime(695): at android.os.Looper.loop(Looper.java:123) 11-09 15:15:57.907: E/AndroidRuntime(695): at android.app.ActivityThread.main(ActivityThread.java:4627) 11-09 15:15:57.907: E/AndroidRuntime(695): at java.lang.reflect.Method.invokeNative(Native Method) 11-09 15:15:57.907: E/AndroidRuntime(695): at java.lang.reflect.Method.invoke(Method.java:521) 11-09 15:15:57.907: E/AndroidRuntime(695): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 11-09 15:15:57.907: E/AndroidRuntime(695): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-09 15:15:57.907: E/AndroidRuntime(695): at dalvik.system.NativeStart.main(Native Method) 11-09 15:15:59.467: I/Process(695): Sending signal. PID: 695 SIG: 9
问题是这样的线
addAmount.setText(aAmt);
aAmt
是一个int
。 这寻找一个resource
与任何一个aAmt
的id
。 你需要先把它转换成一个String
addAmount.setText(String.valueOf(aAmt));
有重载的setText()
方法。 看文档
这篇文章进一步详细说明了这是如何工作的