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

Nyle Cohen
Nyle Cohen
448 Points

I keep getting error messages and I am following along

This is my main activity code

package com.teamrandom.stormy;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 {

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

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

        String apiKey = "9e64526f3fcfdfc5b4461b124ab998ea";
        double latitude = 9999;//37.8267 ;
        double longitude = 122.4233 ;
        String forecastUrl = "https://api.darksky.net/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(Call call, IOException e) {

            }

            @Override
            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, "Exception caught : ", e);
                }

            }
        });
        }

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

    ;
    }

This is my AlertDialogFragment code

package com.teamrandom.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);
                .setTitle(context.getString(R.string.Error_Title))
                .setMessage(getString(R.string.Error_Message))
                .setPositiveButton(getString(R.string.error_ok_button_text), null);

        AlertDialog dialog = builder.create();
        return dialog;
    }
}
//The error is here. It sais it is an else without an if. This is in the MainActivity

if (response.isSuccessful()) {

                        else {
                            alertUserAboutError();
// Here the error sais 'Cannot resolve method'

                .setTitle(context.getString(R.string.Error_Title))
                .setMessage(getString(R.string.Error_Message))
                .setPositiveButton(getString(R.string.error_ok_button_text), null);

3 Answers

Nyle Cohen
Nyle Cohen
448 Points

I managed to solve the error that sais cannot resolve method.

Just change this snippet of code

                .setTitle(context.getString(R.string.Error_Title))
                .setMessage(getString(R.string.Error_Message))
                .setPositiveButton(getString(R.string.error_ok_button_text), null);

To this

builder.setTitle(R.string.Error_title)
                .setMessage(R.string.Error_message)
                .setPositiveButton(R.string.Error_ok_button_text, null);
Nyle Cohen
Nyle Cohen
448 Points

I am still having trouble with the Else without the If

inventor02
inventor02
2,027 Points

Your if statement is not closed. When you make an if statement, you must open it with a { and close it with a }.

E.g.

´´´java if(myCondition) { System.out.println("True!"); } else { System.out.println("Not true :("); } ´´´

Nyle Cohen
Nyle Cohen
448 Points

Thanks! this works now

If you want to chain the methods you shouldn´t put the semicolon when creating the AlertDialogFragment object.

AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(context.getString(R.string.Error_Title))
                .setMessage(getString(R.string.Error_Message))
                .setPositiveButton(getString(R.string.error_ok_button_text), null);

That semicolon was causing the problem