在NewTweetActivity.java中,我有:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.newtweet);
listenToChange();
}
public void listenToChange(){
TextView cEditText = (TextView) findViewById(R.id.tweetCountTextView);
EditText tView = (EditText) findViewById(R.id.tweetComposerEditText);
tView.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
int len = (140 - s.length());
cEditText.setText(len);
}
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
}
但是,它要求我将 cEditText 声明为final,但是当我这样做时,它无法加载newtweet布局。
如果我将 cEditText 声明放在afterTextChanged
中,它会加载布局,但是当我触发afterTextChanged
时(通过输入EditText),它会导致:< / p>
07-08 20:44:27.054: E/AndroidRuntime(4417): FATAL EXCEPTION: main
07-08 20:44:27.054: E/AndroidRuntime(4417): android.content.res.Resources$NotFoundException: String resource ID #0x8b
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.content.res.Resources.getText(Resources.java:201)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.widget.TextView.setText(TextView.java:2911)
07-08 20:44:27.054: E/AndroidRuntime(4417): at com.jgnzapps.quark.NewTweetActivity$1.afterTextChanged(NewTweetActivity.java:31)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.widget.TextView.sendAfterTextChanged(TextView.java:6532)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:6720)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.text.SpannableStringBuilder.sendTextHasChanged(SpannableStringBuilder.java:897)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:353)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:654)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:415)
07-08 20:44:27.054: E/AndroidRuntime(4417): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:318)
07-08 20:44:27.054: E/AndroidRuntime(4417): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:75)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.os.Looper.loop(Looper.java:130)
07-08 20:44:27.054: E/AndroidRuntime(4417): at android.app.ActivityThread.main(ActivityThread.java:3694)
07-08 20:44:27.054: E/AndroidRuntime(4417): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 20:44:27.054: E/AndroidRuntime(4417): at java.lang.reflect.Method.invoke(Method.java:507)
07-08 20:44:27.054: E/AndroidRuntime(4417): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-08 20:44:27.054: E/AndroidRuntime(4417): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-08 20:44:27.054: E/AndroidRuntime(4417): at dalvik.system.NativeStart.main(Native Method)
如果我使用字符串(例如“cats”)调用 cEditText 的setText
方法,则可以正常工作。
有什么想法吗?
答案 0 :(得分:4)
问题很简单:您正在尝试将int设置为Text而不是String。 因此,android执行此方法:http://developer.android.com/reference/android/widget/TextView.html#setText(int) 这意味着,它会搜索具有给定id的String资源,该资源不存在。
您应该以这种方式设置文本:
int len = (140 - s.length());
cEditText.setText(String.valueOf(len));