我有一个定义为class属性的文本视图,以便我可以在整个类中访问它。在onCreate方法中,我执行以下操作:
chars = (TextView) findViewById(R.id.chars);
chars.setText("300");
...后来:
public void onTextChanged(CharSequence s, int start, int before,
int count) {
int max = Integer.parseInt((String)chars.getText());
int current = (message.getText().toString()).length();
chars.setText(max-current);
}
好吧,实际上我只是将300作为字符串检索,将其转换为整数格式并计算最大和当前文本长度之间的差异。然后我想在文本视图中将此值设置为文本。但这失败了
12-29 20:37:06.155: E/AndroidRuntime(2165): FATAL EXCEPTION: main
12-29 20:37:06.155: E/AndroidRuntime(2165): android.content.res.Resources$NotFoundException: String resource ID #0x12b
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.content.res.Resources.getText(Resources.java:201)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.widget.TextView.setText(TextView.java:2857)
12-29 20:37:06.155: E/AndroidRuntime(2165): at com.markupartist.android.actionbar.example.ComposeActivity$3.onTextChanged(ComposeActivity.java:86)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.widget.TextView.sendOnTextChanged(TextView.java:6295)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.widget.TextView.handleTextChanged(TextView.java:6336)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6485)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:654)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:415)
12-29 20:37:06.155: E/AndroidRuntime(2165): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:318)
12-29 20:37:06.155: E/AndroidRuntime(2165): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:75)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.os.Looper.loop(Looper.java:123)
12-29 20:37:06.155: E/AndroidRuntime(2165): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-29 20:37:06.155: E/AndroidRuntime(2165): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 20:37:06.155: E/AndroidRuntime(2165): at java.lang.reflect.Method.invoke(Method.java:507)
12-29 20:37:06.155: E/AndroidRuntime(2165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-29 20:37:06.155: E/AndroidRuntime(2165): at dalvik.system.NativeStart.main(Native Method)
任何想法都错了吗?
答案 0 :(得分:2)
这里发生的是您尝试设置的整数值实际上被视为资源ID。所以chars.setText(String.valueOf(max-current));
应该解决它..
修改强>
另一个注意事项。当您尝试从chars
内的TextView
onTextChanged
上设置文本时,它将再次触发onTextChanged
,这可能会以永无止境的循环结束。
答案 1 :(得分:2)
尝试以下
chars.setText(String.valueOf(max-current));