我想弄清楚的错误来自我的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值[0]中输入一个值时,我会收到这些错误。这是我的logcat:
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)
我该怎么做才能解决这个问题?
答案 0 :(得分:5)
您的textWatcher
为空。你声明它TextWatcher textWatcher;
但你没有初始化它。试试这个:
values[0].addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
答案 1 :(得分:0)
- This error will occur if you pass the null TextWatcher For Ex:
TextWatcher sampleWatcher; EditText sampleEditText;
sampleEditText = (EditText)findViewById(R.id.sample);
sampleEditText.addTextChangedListener(sampleWatcher);
Note : Here sampleWatcher is null. Definition to the sampleWatcher
is missing Solution : sampleWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
} }