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) Concurrency and Error Handling What To Do When the Network is Down

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

the extra challenge for the Toast message !

If you wondering how the extra challenge is done to make the connectivity error shows up dialog instead of toast message. Here how i have done it.

  1. Make new String member variable in the AlertDialogFragment.java named mErrorText
  2. make the setter and getter for the mErrorText variable
  3. Change the setMessage parameter to the getter mErrorText function it will become like this
package net.renotekno.rifqi.cuacaku;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;

public class AlertDialogFragment extends DialogFragment{
    private String mErrorText; // New
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Context context = getActivity();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder
                .setTitle("Oops! Sorry")
                .setMessage(getErrorText()) // New
                .setPositiveButton("OK", null);
        AlertDialog dialog = builder.create();
        return dialog;
    }

    public void setErrorText(String errorText) { // New
        mErrorText = errorText;  
    }

    public String getErrorText() { // New
        return mErrorText;
    }
}
  1. In MainActivity class on the alertUserAboutError function add a String parameter to it to set the error text we want.
  2. After instantiating the new AlertDialogFragment, call the setter which is setErrorText with the function parameter it will become like this
    private void alertUserAboutError(String errorMessage) { // New
        AlertDialogFragment dialog = new AlertDialogFragment();
        dialog.setErrorText(errorMessage); // New
        dialog.show(getFragmentManager(), "error_dialog");
    }
  1. You need to pass in the String parameter when you call that method which is alertUserAboutError("Your error message here")
        alertUserAboutError("There was something wrong. Try again");

If there is any simple idea than this I would be happy to hear that :D.

2 Answers

thanks so much. this helped me grasp the concept of error handling where before I was just following along.

Eugenio Villarreal
Eugenio Villarreal
8,041 Points

Hi, I think this is pretty concise, the only thing I did different was the last part... I used the string resources.

alertUserAboutError(getString(R.string.network_unavailable));