2012-12-27 3 views
0

사용자가 문자열을 입력하는 EditText가 있습니다. 편집 후 문자열이 TextView에 표시됩니다. addTextChangedListener (TextWatcher watcher) 메서드의 매개 변수를 충족시키기 위해 TextWatcher를 구현하는 TextValidator라는 사용자 정의 클래스를 만들었습니다. 내가 그것을 실행할 때 editText에 입력하기 위해 문자를 누르는 순간 app이 충돌합니다. MainActivity에서편집 후 다른 TextView의 EditText 값 표시

:

EditText editText1 = (EditText)findViewById(R.id.editText1); 
editText1.addTextChangedListener(new TextValidator((EditText)findViewById(R.id.editText1))); 

내 TextValidator 클래스에서 :

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); 
    } 
+0

예외가 무엇입니까? –

+0

치명적인 예외 : 주; java.lang.NullPointerException; 이 줄의 내용은 다음과 같습니다. final public void afterTextChanged (편집 가능) { ((EditText) findViewById (R.id.textView1)). setText (editText.getText(). toString()); } – onepiece

답변

2

TextValidator는 활동을 연장하지 않습니다.

솔루션 :

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); 
    } 
    .... 
+0

그러면 afterTextChanged에서 findViewById를 어떻게 호출할까요? – onepiece

+0

나는 내 게시물에 해결책을 넣어 – gezdy

+0

나는 똑같은 일이 발생했다. (앱이 EditText에 문자 나 숫자를 눌렀을 때 멈췄다.) – onepiece