Reputation: 3078
I am attempting to attach a TextWatcher to an EditText. This is done in onCreate(). I know that the EditText is being found successfully. However, when the activity launches, nothing happens when making changes in EditText. I have tried editing the code within the three overridden functions in the TextWatcher to do something besides call a function, but that also has no effect
Relevant code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
setContentView(R.layout.fragment_main);
EditText myEditText = (EditText) findViewById(R.id.cost);
myEditText.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
updatePrice(null);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
updatePrice(null);
}
});
setContentView(R.layout.activity_main);
}
And from the layout:
<EditText
android:id="@+id/cost"
...
android:inputType="numberDecimal" />
Stack trace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.paymeback/com.example.paymeback.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f07003c (com.example.paymeback:id/container) for fragment PlaceholderFragment{b1d7ce60 #0 id=0x7f07003c}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f07003c (com.example.paymeback:id/container) for fragment PlaceholderFragment{b1d7ce60 #0 id=0x7f07003c}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
Upvotes: 0
Views: 1519
Reputation:
You're calling setContentView
three times. Why? You should call it only once. The EditText
you're adding the TextWatcher
to is getting disposed when you replace the content view on your activity on your last call to setContentView
, so whatever EditText
you're typing into when your activity launches, is not the same you added the watcher to.
Upvotes: 2