我试图找出的错误来自我的 textwatcher,但我不知道如何解决它。
public class TipActivity extends Activity implements View.OnClickListener, TextWatcher{
EditText[] values;
Button reset, exit;
TextWatcher textWatcher;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
values = new EditText[4];
values[0] = (EditText)findViewById(R.id.value);
values[1] = (EditText)findViewById(R.id.tip);
values[2] = (EditText)findViewById(R.id.amount);
values[3] = (EditText)findViewById(R.id.fullAmount);
reset = (Button)findViewById(R.id.reset);
exit = (Button)findViewById(R.id.exit);
for(int i = 0; i < values.length; i++)
values[i].setOnClickListener(this);
reset.setOnClickListener(this);
exit.setOnClickListener(this);
values[0].addTextChangedListener(textWatcher);
}
@Override
public void afterTextChanged(Editable e) {
if (!values[0].getText().toString().equals(null)) {
String bill = values[0].getText().toString();
int billValue = Integer.parseInt(bill);
Toast.makeText(getApplicationContext(), "Bill Amount: " + bill, Toast.LENGTH_SHORT).show();
values[3].setText(billValue);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){}
}
当我在我的 Eclipse 模拟器上的 EditText values[0] 中输入一个值时,我得到了这些错误。这是我的日志:
10-24 13:23:13.077: D/AndroidRuntime(4277): Shutting down VM
10-24 13:23:13.077: W/dalvikvm(4277): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-24 13:23:13.127: E/AndroidRuntime(4277): FATAL EXCEPTION: main
10-24 13:23:13.127: E/AndroidRuntime(4277): java.lang.NullPointerException
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.widget.TextView.sendBeforeTextChanged(TextView.java:6280)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.widget.TextView.access$700(TextView.java:192)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.widget.TextView$ChangeWatcher.beforeTextChanged(TextView.java:6478)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.text.SpannableStringBuilder.sendTextWillChange(SpannableStringBuilder.java:878)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:279)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:654)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:180)
10-24 13:23:13.127: E/AndroidRuntime(4277): at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120)
10-24 13:23:13.127: E/AndroidRuntime(4277): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:273)
10-24 13:23:13.127: E/AndroidRuntime(4277): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:75)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.os.Looper.loop(Looper.java:123)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-24 13:23:13.127: E/AndroidRuntime(4277): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 13:23:13.127: E/AndroidRuntime(4277): at java.lang.reflect.Method.invoke(Method.java:507)
10-24 13:23:13.127: E/AndroidRuntime(4277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-24 13:23:13.127: E/AndroidRuntime(4277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-24 13:23:13.127: E/AndroidRuntime(4277): at dalvik.system.NativeStart.main(Native Method)
我能做些什么来解决这个问题?