Reputation: 3529
I have an EditText where the user inputs a string. After edit, the string is displayed in a TextView. I created a custom class called TextValidator which implements TextWatcher, to satisfy the parameters of the addTextChangedListener(TextWatcher watcher) method. When I run it, the moment I press a letter to input into the editText, the app crashes.
In the MainActivity:
EditText editText1 = (EditText)findViewById(R.id.editText1);
editText1.addTextChangedListener(new TextValidator((EditText)findViewById(R.id.editText1)));
In my TextValidator class:
public class TextValidator extends Activity implements TextWatcher {
private EditText editText;
public TextValidator(EditText editText) {
this.editText = editText;
}
public void validate(TextView textView, String text)
{
//I want to later check the string for valid format
}
final public void afterTextChanged(Editable s) {
((EditText)findViewById(R.id.textView1)).setText(editText.getText().toString());
//validate(textView, text);
}
Upvotes: 0
Views: 3715
Reputation: 3322
TextValidator do not extend Activity.
Solution:
public class TextValidator implements TextWatcher {
private Context mContext;
private EditText mEditText;
private TextView mTextView;
public TextValidator(Context mContext) {
this.mContext = mContext;
EditText mEditText = (EditText)mContext.findViewById(R.id.editText1);
TextView mTextView = (TextView)mContext.findViewById(R.id.textView1);
}
public void validate(TextView textView, String text)
{
//I want to later check the string for valid format
}
final public void afterTextChanged(Editable s) {
mTextView.setText(mEditText.getText().toString());
//validate(textView, text);
}
....
Upvotes: 2