Listener in another Thread

Put your problem here if it does not fit any of the other categories.

Listener in another Thread

Postby bsharet » Mon Mar 07, 2011 9:12 am


I have an application that has a GUI and TCP server. The TCP server is running in a different thread and when it receives a certain packet, it should start playing a video file (from MediaPlayer object). The problem is that as a GUI component, the video may be displayed only if invoked from the main thread. I added a listener class to the main class:
Code: Select all
Class RequestListener implements NBRequestListener{  /* NBRequestListener is the interface */
public void onRequestArrived(String request) {
processRequest(request); /* This method will run the video */

I created a listener in the main thread and set it as a listener in the server:
Code: Select all
RequestListener listener = new RequestListener();

The code in the server is:
Code: Select all
public void setRequestListener(_listener) {
listener = _listener; } /* listener is defined as RequestListener */

and the invocation of the event (in the server) is simply:
Code: Select all

But the listener method is invoked in the server thread and not in the main thread and therefore I get exception: java.lang.reflect.InvocationTargetException. Could you please help pointing the problem?

Thanks in advanced.
Once Poster
Once Poster
Posts: 1
Joined: Mon Mar 07, 2011 8:31 am


Re: Listener in another Thread

Postby jonbonazza » Wed Mar 09, 2011 10:15 pm

You will need to use a Handler to post back to the UI thread.
Master Developer
Master Developer
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA


Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google Feedfetcher and 20 guests