Представьте, что у меня есть 5 EditText, и мне нужно заполнить значения, когда изменяется одно значение edittext. Значения взаимозависимы. Для этого я использую следующую логику.
//Finding which edit text is originating values
for(int i=0;i<mEditTextIds.length;i++){
txt[i]=(EditText) findViewById(mEditTextIds[i]);
txt[i].setOnTouchListener(new EditTextTouchLisner());
txt[i].setId(i);
}
//Enable that Edittext's TextChangeListner and disabling all other Edittext's TextChangeListner ..
if (event.getAction() == MotionEvent.ACTION_DOWN){
for(int i=0;i<mTableRowIds.length;i++){
if(i!=v.getId()){
txt[i].addTextChangedListener(null);
}
}
txt[v.getId()].addTextChangedListener(new TextChangedListner());
//calculating values
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//calculate();
Toast.makeText(getApplicationContext(), "CHANGED", Toast.LENGTH_SHORT) .show();
}
Но если я нажимаю edittext второй раз, я получаю следующие ошибки:
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): FATAL EXCEPTION: main
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): java.lang.NullPointerException
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.widget.TextView.sendBeforeTextChanged(TextView.java:6116)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.widget.TextView.access$700(TextView.java:187)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.widget.TextView$ChangeWatcher.beforeTextChanged(TextView.java:6309)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.text.SpannableStringBuilder.sendTextWillChange(SpannableStringBuilder.java:878)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:279)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:583)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:384)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:292)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:73)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.os.Looper.loop(Looper.java:123)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at java.lang.reflect.Method.invoke(Method.java:521)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): at dalvik.system.NativeStart.main(Native Method)