通过实现TextWatcher类使程序崩溃



当我为editText调用addTextChangedListener((方法并实现TextWatcher类并在TextChanged((之后在TextChangeD((上重写3个方法时。程序不会给出任何错误,但程序在实现TextWatcher类时失败,并且在运行应用程序时显示此消息:"不幸的是,My Application1已停止";

这是代码:

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) {
}
}

在运行部分显示以下消息:

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

哪里有问题?

editText.findViewById(R.id.editText(;

它甚至编译。。。你不是说吗

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

(你还有其他的。(

将代码更改为:

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) {
}
}

最新更新