Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I lock an Object / become owner of a monitor (Android)
    text
    copied!<p>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()</p> <p>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.</p> <p>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.</p> <p>One problem I'm having here: I don't understand that...</p> <p>What should I do here to 'pause' a second?</p> <pre><code>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); } } } }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload