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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

[SOLVED] Alert Dialogs not working

Using Android Studio 3.1.2, I've set up my alert dialog but it's not showing up.

MainActivity.java
package uk.co.jonniegrieve.stormy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

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

        String apiKey = "c967557031449620255ff3c64b443a26";

        double latitude = 99999; //37.8267;
        double longitude = -122.4233;

        String forecastURL = "https://api.darksky.net/forecast/"
                + apiKey + "/" + latitude + "," + longitude;



        //The request

        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 {

                    Log.v(TAG, response.body().string());

                    if(response.isSuccessful()) {

                    }
                } catch (IOException e) {
                    alertUserAboutError();
                }
            }
        });

        Log.d(TAG, "Main UI code is running! Hooray");

    }

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

}
AlertDialogFragment.java
package uk.co.jonniegrieve.stormy;

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 {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Context context = getActivity();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        //alert dialog box

        builder.setTitle(R.string.error_title)
        .setMessage(R.string.error_message)
        .setPositiveButton( R.string.error_btn_ok_txt, null);

        return builder.create();

    }
}

I don't understand why the dialogs are not showing up?

https://github.com/jg-digital-media/stormy

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

SOLVED!

I didn't/couldn't see this before but it turns out I wasn't properly handling the exception. I tried to handle it by calling the function which shows the dialog box which wouldn't work.

Simply move the function call inside the else block and properly handle the exception.

public void onResponse(Call call, Response response) throws IOException {

                try {

                    Log.v(TAG, response.body().string());

                    if(response.isSuccessful()) {

                    } else {
                        alertUserAboutError();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "IO Exception Occured!");
                }
            }
        });