gpt4 book ai didi

android - Android 上 mTextEditorWatcher 的问题

转载 作者:太空宇宙 更新时间:2023-11-03 12:39:59 25 4
gpt4 key购买 nike

对之前的“为什么Android这么乱?”表示抱歉。题。我只是对这里的小问题不知所措。我正在尝试制作一个字符。 EditText 上的计数器:

final TextWatcher mTextEditorWatcher = new TextWatcher() {
EditText edit_text = (EditText) findViewById(R.id.editText1);
TextView text_view = (EditText) findViewById(R.id.textView1);

public void beforeTextChanged(CharSequence s, int start, int count, int after) {

};

public void onTextChanged(CharSequence s, int start, int before, int count) {
//This sets a textview to the current length
text_view.setText(String.valueOf(s.length()));
};

//public void afterTextChanged1(Editable s) {
//};

public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub

};
};

然后它崩溃了。这就是为什么我实际上在早些时候询问有关 EditText/TextView 声明的原因。我不明白这段代码有什么问题?

LogCat 日志:

04-23 00:57:46.029: E/AndroidRuntime(347): Uncaught handler: thread main exiting due to uncaught exception
04-23 00:57:46.109: E/AndroidRuntime(347): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dtype.writer/com.dtype.writer.DTypeActivity}: java.lang.NullPointerException
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-23 00:57:46.109: E/AndroidRuntime(347): at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 00:57:46.109: E/AndroidRuntime(347): at android.os.Looper.loop(Looper.java:123)
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-23 00:57:46.109: E/AndroidRuntime(347): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 00:57:46.109: E/AndroidRuntime(347): at java.lang.reflect.Method.invoke(Method.java:521)
04-23 00:57:46.109: E/AndroidRuntime(347): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-23 00:57:46.109: E/AndroidRuntime(347): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-23 00:57:46.109: E/AndroidRuntime(347): at dalvik.system.NativeStart.main(Native Method)
04-23 00:57:46.109: E/AndroidRuntime(347): Caused by: java.lang.NullPointerException
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.Activity.findViewById(Activity.java:1612)
04-23 00:57:46.109: E/AndroidRuntime(347): at com.dtype.writer.DTypeActivity$1.<init>(DTypeActivity.java:29)
04-23 00:57:46.109: E/AndroidRuntime(347): at com.dtype.writer.DTypeActivity.<init>(DTypeActivity.java:28)
04-23 00:57:46.109: E/AndroidRuntime(347): at java.lang.Class.newInstanceImpl(Native Method)
04-23 00:57:46.109: E/AndroidRuntime(347): at java.lang.Class.newInstance(Class.java:1479)
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
04-23 00:57:46.109: E/AndroidRuntime(347): ... 11 more
04-23 00:57:46.128: E/dalvikvm(347): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

此解决方案借自 here Cameron Ketcham 建议使用 mEditText.addTextChangedListener(mTextEditorWatcher); 将 TextWatcher 设置为 EditText。这可能是我问题的核心吗?

2012 年 4 月 24 日更新:仍未找到任何适用于此的解决方案。

最佳答案

我认为那是因为你把:

mTextView = (EditText) findViewById(R.id.textView1);

当它必须是:

mTextView = (TextView) findViewById(R.id.textView1);

关于android - Android 上 mTextEditorWatcher 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10271507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com