2013-01-14 52 views
0

我有上我增加了TextChangedListener四個文本框,下面是我的實現代碼:addTextChangedListener給力閉合

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開發很新。

回答

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()被稱爲前。

+0

是的,這工作像一個魅力......我又如何得到當前選定的文本框?因爲我可以使用開關盒進行不同的操作。 –

+0

嘗試使用'mEditText.getId()'獲取視圖的ID,然後在其上運行開關以查看正在使用哪個。 –

+0

,這給了我一個長整數......這將是相同的跨設備? –

0

您在靜態初始化塊中調用findViewById,該塊在onCreate方法之前調用。因此佈局尚未設置,視圖不存在。

+0

是的謝謝,這是問題... –

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()方法。

+0

很酷..謝謝... –