问题描述
我有 4 个按钮和 1 个 TextView.我希望每个按钮从 TextView 中的值(解析为 int)中添加或减去不同的数量.当我单击其中一个按钮时,它会引发致命错误(如下所列).我搜索了一些其他 Stackoverflow 讨论,但似乎没有任何效果.这是我目前的活动:
I have 4 buttons and 1 TextView. I want each of the buttons to add or subtract a different amount from the value (parsed to int) in the TextView. When I click on one of the buttons it throws fatal errors (listed below). I've searched some other Stackoverflow discussions and nothing seems to be working. Here is my current activity:
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();
}
以下是点击 plusFive 按钮时出现的 logcat:
Here are the logcats that occur on click of the plusFive button:
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
.这将查找具有 id
的 aAmt
的 resource
.您需要先将其转换为 String
像
where aAmt
is an int
. This looks for a resource
with the id
of whatever aAmt
is. You need to cast it to a String
first like
addAmount.setText(String.valueOf(aAmt));
有重载的 setText()
方法.查看文档
这篇关于setText 无法在 TextView 中将数字显示为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!