How can I wait a second in an addTextChangedListener? Following code gives following error: E/AndroidRuntime(1003): java.lang.IllegalMonitorStateException: object not locked by thread before wait()
In the Android reference at method wait() of Object it says: This method can only be invoked by a thread which owns this object's monitor; see notify() on how a thread can become the owner of a monitor.
And at notify() of Object is says: This method can only be invoked by a thread which owns this object's monitor. A thread becomes owner of an object's monitor •by executing a synchronized method of that object; •by executing the body of a synchronized statement that synchronizes on the object; •by executing a synchronized static method if the object is of type Class.
One problem I'm having here: I don't understand that...
What should I do here to 'pause' a second?
public class Spel extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spel);
editText2 = (EditText) findViewById(R.id.editText2);
editText2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (before==0) {
String v = s.toString();
if (v.equals("0") || v.equals("1") || v.equals("2") || v.equals("3") || v.equals("4") || v.equals("5") || v.equals("6") || v.equals("7") || v.equals("8") || v.equals("9")) {
try {
wait(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
editText1.requestFocus();
editText1.setText(null);
editText2.setText(null);
int baanWorpScore = Integer.parseInt(v);
}
else {
// blijf wachten op goede invoer
editText2.setText(null);
}
}
}
});
wait()\notify() methods need to solve consumer\producer problem, when several threads use some shared resource. In your case if you want just wait for a second you need to use Handler and postDelayed. method. If you want just freeze your app for a second use Thread.sleep(1000)