I have four text box on which i am adding a TextChangedListener, Following is the code i implemented:

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

but somehow it gives a force close and i dont know why is it causing the issue. Here is the error log :

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

I am very new to android development.

有帮助吗?

解决方案

Try:

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

You were getting an error because you were trying to use findViewById() before setContentView() was called.

其他提示

You are calling findViewById in the static initializer block, which is called before the onCreate method. therefore the layout isn't set yet and the views don't exist.

move

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

to the onCreate() method.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top