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

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,692 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,403 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.