我有一个文本视图定义为类属性,这样我就可以在整个类中访问它。在 onCreate 方法中,我执行以下操作:
chars = (TextView) findViewById(R.id.chars);
chars.setText("300");
Later...:
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)
有什么想法是错的吗?