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 (2015) Hooking Up the Model to the View Plugging in the Data

updateDisplay() giving FATAL EXCEPTION: Main ?

My onCreate from MainActivity:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ButterKnife.bind(this);


    String apiKey = "*********************************";
    double latitude = 37.8267;
    double longitude = -122.423;
    String forecastURL = "https://api.forecast.io/forecast/" + apiKey + "/" +
                            latitude + "," + longitude;

    if (isNetworkAvailable())
    {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(forecastURL)
                .build();
        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException
            {
                try
                {
                    String jsonData = response.body().string();
                    if (response.isSuccessful()) {
                        mCurrentWeather = getCurrentDetails(jsonData);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                updateDisplay();
                            }
                        });

                    }
                    else
                    {
                        alertUserError();
                    }
                }
                catch (IOException | JSONException e) {
                    Log.e(TAG, "Exception caught: ", e);
                }
            }
        });
    }
    else
    {
        Toast.makeText(this, R.string.networkUnavailable, Toast.LENGTH_LONG).show();
    }
}

And from updateDisplay()

            private void updateDisplay() {

            mTemperatureLabel.setText(String.format("%d", mCurrentWeather.getTemperature()));
            mTimeLabel.setText(String.format("At %d it will be", mCurrentWeather.getTime()));
            mHumidityValue.setText(String.format("%s", mCurrentWeather.getHumidity()));
            mPrecipValue.setText(String.format("%d", mCurrentWeather.getPrecipChance()) + "%");
            mSummaryLabel.setText(mCurrentWeather.getSummary());

            Drawable drawable = ContextCompat.getDrawable(this, mCurrentWeather.getIconID());
            mIconImageView.setImageDrawable(drawable);
}

btw, the error that i got:

FATAL EXCEPTION: main Process: com.mus.xingatempo, PID: 30184 android.content.res.Resources$NotFoundException: String resource ID #0x0 at android.content.res.Resources.getText(Resources.java:312) at android.widget.TextView.setText(TextView.java:4417) at com.mus.xingatempo.MainActivity.updateDisplay(MainActivity.java:104) at com.mus.xingatempo.MainActivity.access$200(MainActivity.java:28) at com.mus.xingatempo.MainActivity$1$1.run(MainActivity.java:81) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 Answer

I got the same error but tried restarting Android Studio, and then it worked. Not really an answer but try restarting Android Studio :)