Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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);

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

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,366 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.