-1

when I call addTextChangedListener() method for editText and I implement TextWatcher class and override 3 methods beforTextChanged() onTextChanged() afterTextChanged(). the program does not give any errors but the program fails whit the implementation of the TextWatcher class and when run the app this message is displayed: "unfortunately, My Application1 has stopped"

this is code:

 package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, TextWatcher {

    private static final String TAG= "MainActivity";
    Button btn1, btn2;
    ImageView imageView;
    EditText editText;
    CheckBox checkBox;
    Switch aSwitch;

    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1=findViewById(R.id.firstButton);
        btn2=findViewById(R.id.secondButton);
        imageView=findViewById(R.id.imageView);
        editText.findViewById(R.id.editText);
        checkBox.findViewById(R.id.checkBox);
        aSwitch.findViewById(R.id.swiClick);

        btn1.setText(R.string.btn);
        btn1.setTextColor(getResources().getColor(R.color.purple_700));
        btn1.setBackgroundColor(getResources().getColor(R.color.teal_700));
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "click btn1");
            }
        });

        btn2.setOnClickListener(this);

        imageView.setScaleType(ImageView.ScaleType.FIT_START);

        Toast.makeText(this,"salam",Toast.LENGTH_SHORT).show();

        editText.addTextChangedListener(this);


    }
    @Override
    public void onClick(View v) {
        Log.d(TAG, "click btn2");
    }


    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        Toast.makeText(this, editText.getText(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}

in run part show these message:

  Install successfully finished in 1 s 492 ms.
$ adb shell am start -n "com.example.myapplication1/com.example.myapplication1.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 1833 on device '-google_nexus_5-192.168.81.101:5555'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
E/libprocessgroup: failed to make and chown /acct/uid_10060: Read-only file system
W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
I/art: Late-enabling -Xcheck:jni
W/System: ClassLoader referenced unknown path: /data/app/com.example.myapplication1-1/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication1, PID: 1833
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication1/com.example.myapplication1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.EditText.findViewById(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.EditText.findViewById(int)' on a null object reference
        at com.example.myapplication1.MainActivity.onCreate(MainActivity.java:36)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
I/Process: Sending signal. PID: 1833 SIG: 9

wheres problem?

  • java.lang.`NullPointerException`: Attempt to invoke virtual method 'android.view.View android.widget.`EditText.findViewById(int)`' on a null object reference – blackapps Feb 07 '22 at 13:46
  • `wheres problem?` -> `Attempt to invoke virtual method 'android.view.View android.widget.EditText.findViewById(int)' on a null object reference` – a_local_nobody Feb 07 '22 at 13:46
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – a_local_nobody Feb 07 '22 at 13:47
  • try use editText = findViewById(R.id.editText); instead of editText.findViewById(R.id.editText); – amir.ashrafi Feb 07 '22 at 14:07

2 Answers2

1

editText.findViewById(R.id.editText);

That it even compiles... Didn't you mean:

editText = findViewById(R.id.editText);? 

(You have other ones..)

blackapps
  • 8,011
  • 2
  • 11
  • 25
0

change your code to this:

 package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, TextWatcher {

    private static final String TAG= "MainActivity";
    Button btn1, btn2;
    ImageView imageView;
    EditText editText;
    CheckBox checkBox;
    Switch aSwitch;

    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1=findViewById(R.id.firstButton);
        btn2=findViewById(R.id.secondButton);
        imageView=findViewById(R.id.imageView);
        editText=findViewById(R.id.editText);
        checkBox=findViewById(R.id.checkBox);
        aSwitch=findViewById(R.id.swiClick);

        btn1.setText(R.string.btn);
        btn1.setTextColor(getResources().getColor(R.color.purple_700));
        btn1.setBackgroundColor(getResources().getColor(R.color.teal_700));
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "click btn1");
            }
        });

        btn2.setOnClickListener(this);

        imageView.setScaleType(ImageView.ScaleType.FIT_START);

        Toast.makeText(this,"salam",Toast.LENGTH_SHORT).show();

        editText.addTextChangedListener(this);


    }
    @Override
    public void onClick(View v) {
        Log.d(TAG, "click btn2");
    }


    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        Toast.makeText(this, editText.getText(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}
OneDev
  • 557
  • 3
  • 14