0

I'm a young computer scientist and I'm learning android development.

I tried since this morning to pass value between 2 inputtext on the same layout. When the user edit one inputtext, I would like to paste the new value to one other inputtext.

This is my code :

private void syncInput() { 
     ((EditText)findViewById(R.id.firstInput)).addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {              
            EditText EditText1 = (EditText)findViewById(R.id.firstInput);
            ((EditText)findViewById(R.id.SecondInput)).setText(EditTextATK.EditText1().toString());

        }        
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }        
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

        }        
    });

}

After the launch of my application, when I edit my firstInput I have this error :

06-22 14:08:25.196: E/AndroidRuntime(519): java.lang.NullPointerException
06-22 14:08:25.196: E/AndroidRuntime(519):  at com.d3ckard.toolbox.activity.MyActivity$3.afterTextChanged(MyActivity.java:71)
06-22 14:08:25.196: E/AndroidRuntime(519):  at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
06-22 14:08:25.196: E/AndroidRuntime(519):  at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:6328)
06-22 14:08:25.196: E/AndroidRuntime(519):  at android.text.SpannableStringBuilder.sendTextHasChanged(SpannableStringBuilder.java:897)
06-22 14:08:25.196: E/AndroidRuntime(519):  at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:353)
06-22 14:08:25.196: E/AndroidRuntime(519):  at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
06-22 14:08:25.196: E/AndroidRuntime(519):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
06-22 14:08:25.196: E/AndroidRuntime(519):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
06-22 14:08:25.196: E/AndroidRuntime(519):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
06-22 14:08:25.196: E/AndroidRuntime(519):  at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:117)

The line number 71 is this one :

((EditText)findViewById(R.id.SecondInput)).setText(EditTextATK.EditText1().toString());
4

3 回答 3

1

改变

((EditText)findViewById(R.id.SecondInput)).setText(EditTextATK.EditText1().toString());

((EditText)findViewById(R.id.SecondInput)).setText(EditText1().getText().toString());
于 2012-06-22T12:30:33.970 回答
0

请试试

private void syncInput() { 
             EditText EditText1 = (EditText)findViewById(R.id.firstInput);
             EditText EditText2 = (EditText)findViewById(R.id.SecondInput);
             EditText1.addTextChangedListener(new TextWatcher() {
                public void afterTextChanged(Editable s) {              

                    EditText2.setText(EditText1.gettext().toString());

                }        
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }        
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                    // TODO Auto-generated method stub

                }        
            });

        }
于 2012-06-22T12:28:56.363 回答
0

您的代码行丢失getText()。它应该是:

((EditText)findViewById(R.id.SecondInput)).setText(EditTextATK.EditText1().getText().toString()); 
于 2012-06-22T12:30:55.910 回答