我有四个文本框,我正在添加一个TextChangedListener,以下是我实现的代码:
public class PasswordActivity extends Activity{
/*This will store the password temporarily */
private int passcodeString;
private String LOG_TAG="Password_Activity";
EditText passOne = (EditText) findViewById(R.id.passcodeOne);
EditText passTwo = (EditText) findViewById(R.id.passcodeTwo);
EditText passThree = (EditText) findViewById(R.id.passcodeThree);
EditText passFour = (EditText) findViewById(R.id.passcodeFour);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.password);
passOne.addTextChangedListener(new CustomTextWatcher(passOne));
}
private class CustomTextWatcher implements TextWatcher {
private EditText mEditText;
public CustomTextWatcher(EditText e) {
mEditText = e;
}
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) {
Log.d(LOG_TAG, s.toString());
}
}}
但不知怎的,它给了一个力量关闭,我不知道为什么它导致了这个问题。这是错误日志:
01-14 21:57:37.396: W/dalvikvm(608): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-14 21:57:37.541: E/AndroidRuntime(608): FATAL EXCEPTION: main
01-14 21:57:37.541: E/AndroidRuntime(608): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.pack.android.email.service/com.pack.android.activity.PasswordActivity}: java.lang.NullPointerException
01-14 21:57:37.541: E/AndroidRuntime(608): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
01-14 21:57:37.541: E/AndroidRuntime(608): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-14 21:57:37.541: E/AndroidRuntime(608): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-14 21:57:37.541: E/AndroidRuntime(608): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-14 21:57:37.541: E/AndroidRuntime(608): at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 21:57:37.541: E/AndroidRuntime(608): at android.os.Looper.loop(Looper.java:123)
01-14 21:57:37.541: E/AndroidRuntime(608): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-14 21:57:37.541: E/AndroidRuntime(608): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 21:57:37.541: E/AndroidRuntime(608): at java.lang.reflect.Method.invoke(Method.java:507)
01-14 21:57:37.541: E/AndroidRuntime(608): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-14 21:57:37.541: E/AndroidRuntime(608): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-14 21:57:37.541: E/AndroidRuntime(608): at dalvik.system.NativeStart.main(Native Method)
01-14 21:57:37.541: E/AndroidRuntime(608): Caused by: java.lang.NullPointerException
01-14 21:57:37.541: E/AndroidRuntime(608): at android.app.Activity.findViewById(Activity.java:1647)
01-14 21:57:37.541: E/AndroidRuntime(608): at com.pack.android.activity.PasswordActivity.<init>(PasswordActivity.java:16)
01-14 21:57:37.541: E/AndroidRuntime(608): at java.lang.Class.newInstanceImpl(Native Method)
01-14 21:57:37.541: E/AndroidRuntime(608): at java.lang.Class.newInstance(Class.java:1409)
01-14 21:57:37.541: E/AndroidRuntime(608): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-14 21:57:37.541: E/AndroidRuntime(608): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
01-14 21:57:37.541: E/AndroidRuntime(608): ... 11 more
我对Android开发很新。
答案 0 :(得分:1)
尝试:
public class PasswordActivity extends Activity{
/*This will store the password temporarily */
private int passcodeString;
private String LOG_TAG="Password_Activity";
EditText passOne;
EditText passTwo;
EditText passThree;
EditText passFour;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.password);
passOne = (EditText) findViewById(R.id.passcodeOne);
passTwo = (EditText) findViewById(R.id.passcodeTwo);
passThree = (EditText) findViewById(R.id.passcodeThree);
passFour = (EditText) findViewById(R.id.passcodeFour);
passOne.addTextChangedListener(new CustomTextWatcher(passOne));
}
private class CustomTextWatcher implements TextWatcher {
private EditText mEditText;
public CustomTextWatcher(EditText e) {
mEditText = e;
}
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) {
Log.d(LOG_TAG, s.toString());
}
}}
您收到错误是因为您在调用findViewById()
之前尝试使用setContentView()
。
答案 1 :(得分:0)
您正在静态初始化程序块中调用findViewById,该块在onCreate方法之前调用。因此布局尚未设置且视图不存在。
答案 2 :(得分:0)
移动
EditText passOne = (EditText) findViewById(R.id.passcodeOne);
EditText passTwo = (EditText) findViewById(R.id.passcodeTwo);
EditText passThree = (EditText) findViewById(R.id.passcodeThree);
EditText passFour = (EditText) findViewById(R.id.passcodeFour);
到onCreate()
方法。