我一直在尝试使用textwatcher。
我从onCreate调用了函数addTextChangedListener(this),但是应用程序由于该命令而崩溃。
请帮忙 。
private EditText txt1;
private EditText txt2;
private EditText txt3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
txt1 = (EditText) findViewById(R.id.signup_username);
txt2 = (EditText) findViewById(R.id.signup_password);
txt3 = (EditText) findViewById(R.id.signup_email);
txt1.addTextChangedListener(this);
txt2.addTextChangedListener(this);
txt3.addTextChangedListener(this);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
和:
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
View view = getCurrentFocus();
int id = view.getId();
if (id == R.id.signup_username) {
String username = ((EditText) view).getText().toString();
for (int i = 0; i < username.length(); i++) {
if (username.charAt(i) < 'a' || username.charAt(i) > 'z') {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
break;
}
else {
Toast.makeText(getApplicationContext(), "Murad", Toast.LENGTH_SHORT).show();
break;
}
}
}
else {
Toast.makeText(getApplicationContext(), "Oren", Toast.LENGTH_SHORT).show();
}
}
}
日志:
04-03 11:37:22.684: D/skia(23439): new locale en-Latn-GB
04-03 11:37:22.854: D/libEGL(23439): loaded /vendor/lib/egl/libEGL_adreno.so
04-03 11:37:22.854: D/libEGL(23439): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
04-03 11:37:22.864: D/libEGL(23439): loaded /vendor/lib/egl/libGLESv2_adreno.so
04-03 11:37:22.864: I/Adreno-EGL(23439): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: (CL4169980)
04-03 11:37:22.864: I/Adreno-EGL(23439): OpenGL ES Shader Compiler Version: 17.01.10.SPL
04-03 11:37:22.864: I/Adreno-EGL(23439): Build Date: 09/26/13 Thu
04-03 11:37:22.864: I/Adreno-EGL(23439): Local Branch:
04-03 11:37:22.864: I/Adreno-EGL(23439): Remote Branch:
04-03 11:37:22.864: I/Adreno-EGL(23439): Local Patches:
04-03 11:37:22.864: I/Adreno-EGL(23439): Reconstruct Branch:
04-03 11:37:22.914: D/OpenGLRenderer(23439): Enabling debug mode 0
04-03 11:37:39.971: D/AndroidRuntime(23439): Shutting down VM
04-03 11:37:39.971: W/dalvikvm(23439): threadid=1: thread exiting with uncaught exception (group=0x41b2b898)
04-03 11:37:39.981: E/AndroidRuntime(23439): FATAL EXCEPTION: main
04-03 11:37:39.981: E/AndroidRuntime(23439): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.poca2/com.example.poca2.RegisterActivity}: java.lang.NullPointerException
04-03 11:37:39.981: E/AndroidRuntime(23439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
04-03 11:37:39.981: E/AndroidRuntime(23439): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-03 11:37:39.981: E/AndroidRuntime(23439): at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-03 11:37:39.981: E/AndroidRuntime(23439): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-03 11:37:39.981: E/AndroidRuntime(23439): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 11:37:39.981: E/AndroidRuntime(23439): at android.os.Looper.loop(Looper.java:137)
04-03 11:37:39.981: E/AndroidRuntime(23439): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-03 11:37:39.981: E/AndroidRuntime(23439): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 11:37:39.981: E/AndroidRuntime(23439): at java.lang.reflect.Method.invoke(Method.java:525)
04-03 11:37:39.981: E/AndroidRuntime(23439): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-03 11:37:39.981: E/AndroidRuntime(23439): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-03 11:37:39.981: E/AndroidRuntime(23439): at dalvik.system.NativeStart.main(Native Method)
04-03 11:37:39.981: E/AndroidRuntime(23439): Caused by: java.lang.NullPointerException
04-03 11:37:39.981: E/AndroidRuntime(23439): at com.example.poca2.RegisterActivity.onCreate(RegisterActivity.java:33)
04-03 11:37:39.981: E/AndroidRuntime(23439): at android.app.Activity.performCreate(Activity.java:5372)
04-03 11:37:39.981: E/AndroidRuntime(23439): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-03 11:37:39.981: E/AndroidRuntime(23439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
04-03 11:37:39.981: E/AndroidRuntime(23439): ... 11 more
04-03 11:37:44.545: D/skia(23761): new locale en-Latn-GB
04-03 11:37:44.776: D/libEGL(23761): loaded /vendor/lib/egl/libEGL_adreno.so
04-03 11:37:44.786: D/libEGL(23761): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
04-03 11:37:44.786: D/libEGL(23761): loaded /vendor/lib/egl/libGLESv2_adreno.so
04-03 11:37:44.786: I/Adreno-EGL(23761): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: (CL4169980)
04-03 11:37:44.786: I/Adreno-EGL(23761): OpenGL ES Shader Compiler Version: 17.01.10.SPL
04-03 11:37:44.786: I/Adreno-EGL(23761): Build Date: 09/26/13 Thu
04-03 11:37:44.786: I/Adreno-EGL(23761): Local Branch:
04-03 11:37:44.786: I/Adreno-EGL(23761): Remote Branch:
04-03 11:37:44.786: I/Adreno-EGL(23761): Local Patches:
04-03 11:37:44.786: I/Adreno-EGL(23761): Reconstruct Branch:
04-03 11:37:44.836: D/OpenGLRenderer(23761): Enabling debug mode 0
04-03 11:37:48.069: D/AndroidRuntime(23761): Shutting down VM
04-03 11:37:48.069: W/dalvikvm(23761): threadid=1: thread exiting with uncaught exception (group=0x41b2b898)
04-03 11:37:48.069: E/AndroidRuntime(23761): FATAL EXCEPTION: main
04-03 11:37:48.069: E/AndroidRuntime(23761): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.poca2/com.example.poca2.RegisterActivity}: java.lang.NullPointerException
04-03 11:37:48.069: E/AndroidRuntime(23761): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
04-03 11:37:48.069: E/AndroidRuntime(23761): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-03 11:37:48.069: E/AndroidRuntime(23761): at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-03 11:37:48.069: E/AndroidRuntime(23761): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-03 11:37:48.069: E/AndroidRuntime(23761): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 11:37:48.069: E/AndroidRuntime(23761): at android.os.Looper.loop(Looper.java:137)
04-03 11:37:48.069: E/AndroidRuntime(23761): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-03 11:37:48.069: E/AndroidRuntime(23761): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 11:37:48.069: E/AndroidRuntime(23761): at java.lang.reflect.Method.invoke(Method.java:525)
04-03 11:37:48.069: E/AndroidRuntime(23761): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-03 11:37:48.069: E/AndroidRuntime(23761): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-03 11:37:48.069: E/AndroidRuntime(23761): at dalvik.system.NativeStart.main(Native Method)
04-03 11:37:48.069: E/AndroidRuntime(23761): Caused by: java.lang.NullPointerException
04-03 11:37:48.069: E/AndroidRuntime(23761): at com.example.poca2.RegisterActivity.onCreate(RegisterActivity.java:33)
04-03 11:37:48.069: E/AndroidRuntime(23761): at android.app.Activity.performCreate(Activity.java:5372)
04-03 11:37:48.069: E/AndroidRuntime(23761): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-03 11:37:48.069: E/AndroidRuntime(23761): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
04-03 11:37:48.069: E/AndroidRuntime(23761): ... 11 more
04-03 11:37:54.265: I/Process(23761): Sending signal. PID: 23761 SIG: 9
编辑:
private EditText txt1;
private EditText txt2;
private EditText txt3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
txt1 = (EditText) findViewById(R.id.signup_username);
txt2 = (EditText) findViewById(R.id.signup_password);
txt3 = (EditText) findViewById(R.id.signup_email);
txt1.addTextChangedListener(new CustomTextWatcher(txt1, getApplicationContext()));
txt2.addTextChangedListener(new CustomTextWatcher(txt2, getApplicationContext()));
txt3.addTextChangedListener(new CustomTextWatcher(txt3, getApplicationContext()));
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
class CustomTextWatcher implements TextWatcher {
EditText m;
Context c;
public CustomTextWatcher (EditText e, Context context) {
m = e;
c = context;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
int id = m.getId();
if (id == R.id.signup_username) {
String username = ((EditText) m).getText().toString();
for (int i = 0; i < username.length(); i++) {
if (username.charAt(i) < 'a' || username.charAt(i) > 'z') {
Toast.makeText(c, "Error", Toast.LENGTH_SHORT).show();
break;
}
else {
Toast.makeText(c, "Murad", Toast.LENGTH_SHORT).show();
break;
}
}
}
else {
Toast.makeText(c, "Oren", Toast.LENGTH_SHORT).show();
}
}
}
这是编辑版。
最佳答案
您的EditText
不在activity_register
布局中,而是在片段中,因此在仅夸大findViewById()
布局后无法在activity_register
中找到。当您尝试在空引用上调用方法时,这会导致NPE。
将findViewById()
和addTextChangedListener()
代码移动到片段的onCreateView()
上,您实际上是在其中放大包含编辑文本的视图。
关于java - 创建时使用Text Watcher android,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22832042/