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

Ghadeer Shaaya
Ghadeer Shaaya
1,522 Points

A Toast can't be used instead of Log

I tried using a Toast in addition to logging the data but it is causing the app to crash.

Here is the method:

'''Android

private CurrentWeather getCurrentDetails(String jsonData) throws JSONException{

    JSONObject forecast = new JSONObject(jsonData);

    String temp = forecast.getString("timezone");

    Log.i(TAG, temp);

    // Debugging shows that the following line is what causing the problem

    Toast.makeText(MainActivity.this, temp, Toast.LENGTH_LONG).show();

    return new CurrentWeather();

}

'''

Simon Coates
Simon Coates
28,694 Points

Can you specify where this is from? I think when requesting help you're able to link to where you hit the problem. Helps people debug the code if they know where it's from. I'm not familiar with android development, but the third parameter looks okay. I'd verify that the first two are what you think they are (non-null). If your parameters are okay, then the problem might be more fundamental.

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

That small of a snippet isn't enough to diagnose the cause. Are you calling this from within runOnUiThread?

Ghadeer Shaaya
Ghadeer Shaaya
1,522 Points

I am sorry I thought my question will be somehow connected to the lecture it came from. You answered my question actually, this function is being call from a background thread and I was trying to show a Toast on main activity. This is what is causing the issue. Thanks.