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 Building an Alert Dialog

Carney Bernard
Carney Bernard
3,615 Points

Code looks 100% correct yet I am still getting errors... Could someone else please take a look?

My code is pretty much the same format Ben demonstrated in the video. Not sure what's going on. If someone could take a look and tell me what they think that would be great! The objectives were literally to set the text of the AlertDialog box. Not sure if the error is because my strings are hard coded or if it is another minor mistake...

AlertDialogFragment.java
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);
                    builder.setTitle("Sorry!")
                    .setMessage("Try again")
                    .setButtonText("OK")
                    .setButtonListner(null);



        return null;
    }
}
MovieActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;

public class MovieActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie);

        // Get some movie information!
        String apiUrl = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=xyz&q=hobbit";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(apiUrl)
                .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 { }
        });
    }
}

2 Answers

Challenge Link: http://teamtreehouse.com/library/build-a-weather-app/concurrency-and-error-handling/building-an-alert-dialog

Challenge Task 2 of 4

Next, set the title, message, and positive button of the builder. Use the following values: title = "Sorry!", message = "Try again!", button text = "OK", button listener = null.

This particular challenge is really really picky!

Even after you clear the parse errors

if you forget a "!" or say "Okay" instead of "OK" it won't pass.

You have to use code like this to get it to pass:

import android.content.Context;
import android.os.Bundle;

public class AlertDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Context context = getActivity();

        // Insert code here!
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
          builder.setTitle("Sorry!");
          builder.setMessage("Try again!");
          builder.setPositiveButton("OK", null); 
        return null;
    }
}

..and most of the example you found "in the wild"

(in real life, not a fake teaching exercise)

use something like this for '.setPositiveButton":

builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User clicked OK button
           }
       });

The code above, although standard elsewhere, is NOT what the challenge is looking for.

It's actually from the Androids Developers site, though: http://developer.android.com/guide/topics/ui/dialogs.html#AddingButtons

...and since I didn't see any threads on parts 3 and 4 of the challeneg:

Challenge Task 3 of 4

Our builder is ready! Add a new AlertDialog variable and create it from the builder. Then return it (instead of null).

You just need to replace the line:

return null;

...with this line:

return builder.create();

Final challenge is:

Challenge Task 4 of 4

Let's finally use AlertDialogFragment! Switch to the Activity file and create and show a new AlertDialogFragment in the onFailure() method. Use getFragmentManager() and "error_dialog" as the two parameters for the method that shows it.

If you look at these two threads:

https://teamtreehouse.com/forum/networkonmainthreadexception

https://teamtreehouse.com/forum/okhttp-responsebody-can-not-be-converted-to-a-string-please-help

...you'll see the same private void code:

    private void alertUserAboutError() {

        AlertDialogFragment alert = new AlertDialogFragment();
        alert.show(getFragmentManager(), "error_dialog");
    }

However to pass the part 4 of 4 challenge we only need the inside two lines:

        AlertDialogFragment alert = new AlertDialogFragment();
        alert.show(getFragmentManager(), "error_dialog");

The big question is where to put them?

After playing around I finally figured it out.

Try this for MovieActivity.java (...and see if it passes.. :thumbsup:):

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;

public class MovieActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie);

        // Get some movie information!
        String apiUrl = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=xyz&q=hobbit";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(apiUrl)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
          public void onFailure(Request request, IOException e) {
                 AlertDialogFragment dialog = new AlertDialogFragment();
                 dialog.show(getFragmentManager(), "error_dialog");
            }

            @Override
            public void onResponse(Response response) throws IOException { }
        });
    }
}

You're the boss!!! (o baby a triple "!!!") It worked man

I did not expect this jackpot of answers It's like a pile of answers....

Don't I sound like a pokemon black and white Answer con-na-su-wer

Don't know how to spell it THANKS AGAIN

Bye!

Jesse Gravenor
Jesse Gravenor
27,272 Points

Instead of a .setButtonListener, you need a set positive button with text of OK and listener to null.