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

General Discussion

The activity life cycle code challenge - question # 2

Hello,

I'm stuck on the life cycle code challenge question #2. This is under the android development track > build a simple android app > shaking things up > Activity Life Cycle

The question is as follows:

A variable named 'otherString' is declared and set in the onCreate() method of the Activity class (the superclass of this class). Make the sentence for the 'aString' variable true by replacing the word 'UNKNOWN' with the correct word ('BEFORE' or 'AFTER').

The first sentence of this question is confusing. Do I need to create and instantiate otherString or has that already been done somewhere? I also don't understand what is meant on the second sentence by making the sentence variable true.

The error I get is: Bummer! Compilation Error! When editing a String, make sure it starts and ends with double-quotes and is all on the same line. Type carefully!

My code is:

package com.example;

import android.os.Bundle; import android.view.View; import android.hardware.Sensor; import android.hardware.SensorManager;

public class MainActivity extends Activity {

public String aString;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private ShakeDetector mShakeDetector;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

    aString = "BEFORE";

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mShakeDetector = new ShakeDetector();
}

aString = "BEFORE";

Note that I remove the word "static" when aString is declared and then try to set it's value to "BEFORE."

Please help.

2 Answers

A variable called otherString has been created in MainActivity's superclass (the Activity class). For the purposes of this question, you don't need to do anything with the otherString variable. You just need to figure out when it gets assigned a value. Here's a breakdown of what the question is asking.

  • There is a variable called otherString in the Activity class. It gets assigned a value once the onCreate method of the Activity class gets called. It doesn't matter what the value is.

  • Since MainActivity is a subclass of Activity, you are calling the onCreate method of Activity when you say super.onCreate(savedInstanceState)

  • Inside of onCreate, the code for the challenge is assigning a value to the variable aString. Does that assignment happen before or after otherString gets assigned a value? You have to replace the word "UNKNOWN" with either the word "BEFORE" or the word "AFTER" in the variable aString so that sentence is true. The only code you should modify for this question is to change the word "UNKNOWN" inside of the string to either "BEFORE" or "AFTER".

silly quiz... I was over doing it here. This is the first time you guys ask me to update text within a string plus I was also thrown off my the word static when the variable was declared. Thanks so much for the assistance.