0

我有四个文本框,我在其中添加了一个 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开发很陌生。

4

3 回答 3

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()

于 2013-01-14T17:33:51.400 回答
0

您在静态初始化程序块中调用 findViewById,该程序块在 onCreate 方法之前调用。因此布局尚未设置,视图不存在。

于 2013-01-14T17:33:55.610 回答
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()方法。

于 2013-01-14T17:34:09.323 回答