NXT-Spy – Configuration of the transfer status picto

I am now working on the little picto that indicates the transfer status between the three states of the connection.

Something that I thought would take a couple of minutes finally took me 5 hours!
First of all, to make the code as clean as possible, I implemented a onChange event in the ServerSocket class [source].

Then, in the TransferStatusChangeListener, I simply called the transfer status ImageView component. And I got a really unexpected exception : “CalledFromWrongThreadException : Only the original thread that created a view hierarchy can touch its views”. Which means that you cannot edit a UI component if you are not in the main Thread.

I have searched for hours for a solution on Google because everyone was leading me the wrong way : On all the forums, I was told to use AsyncTask to solve my problem but this class was really not appropriated to my problem : AsyncTask allows to have access to a thread from an other one but can only be called once and have to be created from the class we want to access.

I finally found the solution in the Activity reference [source] : runOnUiThread that allows any thread to access the UI at any moment as long as we have a reference of the main Activity.

One thought on “NXT-Spy – Configuration of the transfer status picto

  1. Pingback: More problems about active components solved…NXT-Spy | NXT-Spy

Leave a Reply

Your email address will not be published. Required fields are marked *