Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App Hooking Up the Model to the View Weather Icon

Santiago Serrano
Santiago Serrano
2,754 Points

Setting the drawable to iconImageView in the background thread doesn't crash my app

Hello everyone! I tried replicating the app crash caused by touching the view in the background thread on my emulator, but surprisingly, the app doesn't crash, and the correct icon is displayed.

It may be worth noting that I made a small change in the code inside the if(response.isSuccessful()){} block. I thought having a "displayWeather" object was unnecessary since I could just put the "currentWeather" object inside the binding.setWeather() method. Here's my code in there:

if (response.isSuccessful()) {
      currentWeather = getCurrentDetails(jsonData);
      binding.setWeather(currentWeather);

      Drawable drawable = getResources().getDrawable(currentWeather.getIconID());
      iconImageView.setImageDrawable(drawable);

Anyways, that doesn't detract from the fact that I'm still modifying a view made in the main thread in the background thread. Another thing that might be worth noting is that I'm using Android Studio 3.2.1

Santiago Serrano
Santiago Serrano
2,754 Points

Also, when using the runOnUiThread() method, it doesn't require me to make "currentWeather" final.

2 Answers

Benjamin Gooch
Benjamin Gooch
20,367 Points

I'm getting the same result. Everything is as instructed, so maybe it's just the newer version of Andriod? I'm on 3.5.

i get the same result with 3.5 but can't find any trace of this being removed in the later releases. The docs still suggest the non-UI threads can't access the main thread.

Very odd.

Steve.