How to implement wait(); to wait for a notifyAll(); from enter button?
Posted
by
Dakota Miller
on Stack Overflow
See other posts from Stack Overflow
or by Dakota Miller
Published on 2013-06-27T02:15:52Z
Indexed on
2013/06/27
4:21 UTC
Read the original article
Hit count: 241
Sorry for the confusion I posted the Worng Logcat info. I updated the question. I want to click Start to start a thread then when enter is clicked i want the thad to continue and get the message and handle the message in the thread then output it to the main thread and update the text view. How would i start a thread to wait for enter to be pressed and get the bundle for the Handler? Here is my Code:
public class MainActivity extends Activity implements OnClickListener {
Handler mHandler;
Button enter;
Button start;
TextView display;
String dateString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enter = (Button) findViewById(R.id.enter);
start = (Button) findViewById(R.id.start);
display = (TextView) findViewById(R.id.Display);
enter.setOnClickListener(this);
start.setOnClickListener(this);
mHandler = new Handler() { <=============================This is Line 31
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Bundle bundle = msg.getData();
String string = bundle.getString("outKey");
display.setText(string);
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.enter:
Message msgin = Message.obtain();
Bundle bundlein = new Bundle();
String in = "It Works!";
bundlein.putString("inKey", in);
msgin.setData(bundlein);
notifyAll();
break;
case R.id.start:
new myThread().hello.start();
break;
}
}
public class myThread extends Thread {
Thread hello = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Handler Mhandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Bundle bundle = msg.getData();
dateString = bundle.getString("inKey");
}
};
Looper.loop();
Message msg = Message.obtain();
Bundle bundle = new Bundle();
bundle.putString("outKey", dateString);
msg.setData(bundle);
mHandler.sendMessage(msg);
}
};
}
}
Here is the logcat info:
06-27 00:00:24.832: E/AndroidRuntime(18513): FATAL EXCEPTION: Thread-1210
06-27 00:00:24.832: E/AndroidRuntime(18513): java.lang.IllegalMonitorStateException: object not locked by thread before wait()
06-27 00:00:24.832: E/AndroidRuntime(18513): at java.lang.Object.wait(Native Method)
06-27 00:00:24.832: E/AndroidRuntime(18513): at java.lang.Object.wait(Object.java:364)
06-27 00:00:24.832: E/AndroidRuntime(18513): at com
.example.learninghandlers.MainActivity$myThread$1.run(MainActivity.java:77)
© Stack Overflow or respective owner