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 Configuring the Alert Dialog

app not crushing when I set wrong latitude

my app is not displaying the crash message when I set the wrong latitude, what might the problem be

there is my mainactivity:

public class MainActivity extends ActionBarActivity { public static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String apiKey ="feebc37394effc0ee0b7c5b637d9f7b8";
    double latitude = 9999;//37.8267;
    double longitude = -122.423;

    String forecastUrl = "https://api.forecast.io/forecast/"+ apiKey +"/,"+ latitude +", " + longitude;
    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(Request request, IOException e) {

        }

        @Override
        public void onResponse(Response response) throws IOException {
            try {
                Log.v(TAG, response.body().string());

                if (response.isSuccessful()) {

                }
                else {
                    alertAboutError();


                }

            } catch (IOException e) {
                Log.e(TAG, "Exception Caught: ", e);
            }

        }
    });

}

private void alertAboutError() {
    AlertDialogFragment dialog = new AlertDialogFragment();
    dialog.show(getFragmentManager(), "error_dialog");
}

} and that's my startDialogue fragment class

public class AlertDialogFragment extends DialogFragment{ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Context context = getActivity(); AlertDialog.Builder builder =new AlertDialog.Builder(context) .setTitle(context.getString(R.string.error_title)).setMessage(context.getString(R.string.error_message)) .setPositiveButton(context.getString(R.string.error_ok_button_text), null); AlertDialog dialog = builder.create(); return dialog; } }

2 Answers

@Override public void onFailure(Request request, IOException e) { alertAboutError(); }

I noticed that the code in this lesson only results in an error when run on an emulator. I use a real device when working through these lessons, and I couldn't figure out why I wasn't getting an error. In order for the error to show up on my real device, I had to add the code that Mustafa provided above to the onFailure() method in MainActivity.java.

So my onFailure() method ends up looking like this:

public void onFailure(Request request, IOException e) {

     alertUserAboutError();

}

UPDATE: The WiFi on my real device was turned off! Not sure how that happened, but when I turned it on again (no data plan on this device) I was able to see the dialog without specifically calling alertUserAboutError() in the onFailure() method.

Kyle Baker
Kyle Baker
8,211 Points

sweet, helped me ty