Home » Android » java – issue with removeTextChangedListener(this) and addTextChangedListener(this)-Exceptionshub

java – issue with removeTextChangedListener(this) and addTextChangedListener(this)-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I am using Android Studio and writing in Java. I don’t understand why I am having this issue. Thank you!
enter image description here

I was following the step from this video.
https://www.youtube.com/watch?v=BtFI19L-xdY&t=332s

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

}

public void onTextChanged(CharSequence s, int start, int before, int count){

}

public void afterTextChanged(Editable s){
    String string = s.toString().trim();
    if (string.isEmpty()) {
        resetMessageEdit();
    }else {
        sendBtn.setVisibility(View.VISIBLE);
        pickImgBtn.setVisibility(View.INVISIBLE);
    }
}

private void resetMessageEdit(){
    messageEdit.removeTextChangedListener(this);
    messageEdit.setText("");
    sendBtn.setVisibility(View.INVISIBLE);
    pickImgBtn.setVisibility(View.VISIBLE);
    messageEdit.addTextChangedListener(this);
}

It gives me an error when I point on the red error part.

removeTextChangedListener (android.text.TextWatcher) in TextView
cannot be applied to
(com.example.mywebsocketapplication.ChatActivity)
or
addTextChangedListener (android.text.TextWatcher) in TextView cannot
be applied to (com.example.mywebsocketapplication.ChatActivity)

After I run, the error display below will be
C:\Users\zhaox\OneDrive\Desktop\resume\MyWebsocketApplication\app\src\main\java\com\example\mywebsocketapplication\ChatActivity.java:326: error: class, interface, or enum expected
*/
^
1 error

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ‘:app:compileDebugJavaWithJavac’.

> Compilation failed; see the compiler error output for details.

  • Try:
    Run with –stacktrace option to get the stack trace. Run with –info or –debug option to get more log output. Run with –scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 0s

How to&Answers: