gpt4 book ai didi

android - Edittext addTextChangedListener 崩溃

转载 作者:行者123 更新时间:2023-12-02 22:55:59 26 4
gpt4 key购买 nike

当我开始此 Activity 时,我的应用程序崩溃了。我在布局中定义了一个编辑文本,并在 Activity 的 onCreate 中实例化。我正在为我的 Edittext 创建监听器。

mTextView = (EditText) findViewById(R.id.edit_text);
mTextView.setInputType(InputType.TYPE_CLASS_TEXT);

mTextView.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {

}

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

}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}
});

最佳答案

如果没有堆栈跟踪,很难判断问题是什么。如果我不得不猜测,我会说当您调用 mTextView.setInputType(InputType.TYPE_CLASS_TEXT); 时,您会收到 NullPointerException。这可能是由于 findViewById 返回 null 导致的,因为它在您的 Activity 中找不到任何具有给定 id 的 View 。您打过电话吗setContentView(int layoutId)在您发布的代码之前?另外,如果您调用了 setContentView,您确定您在该调用中膨胀的布局实际上包含一个 ID 为 edit_text 的 EditText 小部件吗?

关于android - Edittext addTextChangedListener 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8001381/

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