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

Barnabas Elogie
PLUS
Barnabas Elogie
Courses Plus Student 463 Points

my weather app always crash and when i try to check the errors its show me this line of code.ButterKnife.inject(this);

public class MainActivity extends AppCompatActivity { public static final String TAG = MainActivity.class.getSimpleName(); private CurrentWeather mCurrentWeather; /* private TextView mTemparetureLabel; private TextView mTimeLebel; private TextView mHumidityValue; private TextView mPricpValue; private TextView mSummaryLebel; private TextView miconimageView; private ImageView mRefreshImageView;

private ProgressBar mProgressBar;*/

@InjectView(R.id.TimeLebel)
TextView mTimeLebel;
@InjectView(R.id.TemparetureLabel)
TextView mTemparetureLabel;
@InjectView(R.id.HumidityValue)
TextView mHumidityValue;
@InjectView(R.id.PricipValue)
TextView mPricipValue;
@InjectView(R.id.SummaryLabel)
TextView mSummaryLabel;
@InjectView(R.id.iconimageView)
TextView mIconimageView;
@InjectView(R.id.RefreshImageView)
ImageView mRefreshImageView;
@InjectView(R.id.ProgressBar)
ProgressBar mProgressBar;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient mClient;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);
   /* mTemparetureLabel = (TextView)findViewById(R.id.TemparetureLabel);
    mTimeLebel = (TextView)findViewById(R.id.TimeLebel);
    mHumidityValue = (TextView)findViewById(R.id.HumidityValue);
    mPricpValue =  (TextView)findViewById(R.id.PricipValue);
    mSummaryLebel = (TextView)findViewById(R.id.SummaryLabel);
    mRefreshImageView = (ImageView)findViewById(R.id.RefreshImageView);
    mProgressBar = (ProgressBar)findViewById(R.id.mProgressBar);
   // mProgressBar.setVisibility(View.INVISIBLE);*/


    final double Longitude = 37.8267;
    final double latitude = -122.423;
    mRefreshImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getForcast(latitude, Longitude);

        }
    });


    getForcast(latitude, Longitude);

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

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

private void getForcast(double latitude, double Longitude) {
    String ApiKey = "8232e844c7098062c5254fd9901c80d7";

    String ForcastUrl = "https://api.forecast.io/forecast/" + ApiKey + "/" + latitude + "," + Longitude;
    if (isNetworkAvailable()) {
        toggleRefresh();

        OkHttpClient Client = new OkHttpClient();
        Request request = new Request.Builder().url(ForcastUrl).build();
        Call call = Client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        toggleRefresh();

                    }
                });
                alertUserAboutErro();

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        toggleRefresh();

                    }
                });
                try {
                    String jsonData = response.body().string();
                    Log.v(TAG, jsonData);


                    if (response.isSuccessful()) {
                        mCurrentWeather = getCurrentDetails(jsonData);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                UpdateDisplay();

                            }
                        });


                    } else {
                        alertUserAboutErro();
                    }

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


            }
        });
    } else {
        Toast.makeText(this, R.string.network_unavailable, Toast.LENGTH_LONG).show();
    }


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

private void toggleRefresh() {
    if (mProgressBar.getVisibility() == View.INVISIBLE) {
        mProgressBar.setVisibility(View.VISIBLE);
        mRefreshImageView.setVisibility(View.INVISIBLE);
    } else {
        mProgressBar.setVisibility(View.INVISIBLE);
        mRefreshImageView.setVisibility(View.VISIBLE);

    }

}

private void UpdateDisplay() {
    mTemparetureLabel.setText(mCurrentWeather.getTempareture() + "");
    mTimeLebel.setText("At" + mCurrentWeather.getFormatedTime());
    mHumidityValue.setText(mCurrentWeather.getHumidity() + "");
    mPricipValue.setText(mCurrentWeather.getPricipChance() + "");
    mSummaryLabel.setText(mCurrentWeather.getSumary());


}

private CurrentWeather getCurrentDetails(String jsonData) throws JSONException {
    JSONObject forecast = new JSONObject(jsonData);
    String timezone = forecast.getString("timezone");
    Log.i(TAG, "from JSON" + timezone);
    JSONObject Currently = forecast.getJSONObject("Currently");
    CurrentWeather currentWeather = new CurrentWeather();
    CurrentWeather.setHumidity(Currently.getDouble("humidity"));
    CurrentWeather.setTime(Currently.getLong("Time"));
    CurrentWeather.setIcon(Currently.getString("icon"));
    CurrentWeather.setPricipChance(Currently.getDouble("PriciProbability"));
    CurrentWeather.setSumary(Currently.getString("summary"));
    CurrentWeather.setTempareture(Currently.getDouble("tempareture"));
    CurrentWeather.setTimeZone(Currently.getString("TimeZone"));

    Log.d(TAG, CurrentWeather.getFormattedTime());
    return new CurrentWeather();
}

private boolean isNetworkAvailable() {
    ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    boolean isNetworkAvailable = false;
    if (networkInfo != null && networkInfo.isConnected()) {
        isNetworkAvailable = true;

    }
    return isNetworkAvailable;
}

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


@Override
public void onStart() {
    super.onStart();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    mClient.connect();
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Main Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is correct.
            Uri.parse("android-app://com.bosoomama.forcast/http/host/path")
    );
    AppIndex.AppIndexApi.start(mClient, viewAction);
}

@Override
public void onStop() {
    super.onStop();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Main Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is correct.
            Uri.parse("android-app://com.bosoomama.forcast/http/host/path")
    );
    AppIndex.AppIndexApi.end(mClient, viewAction);
    mClient.disconnect();
}

} // please can someone help.

1 Answer

The video shows an older version of ButterKnife that uses the @InjectView and ButterKnife.inject(this);

The newer version of ButterKnife uses @Bind and ButterKnife.bind(this);