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

Code Challenge: The Activity Lifecycle

Hello

I have a code challenge and I am some what stuck

Question:

Inside the 'onCreate()' method of MainActivity, add a line of code to execute the 'onCreate()' method of the parent, or superclass, of this MainActivity class. Don’t forget to pass along the parameter, and call it before the 'aString' variable is set!

My Answer :

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 static String aString;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate () {aString};

        setContentView(R.layout.activity_main); 

        aString = "This variable is set UNKNOWN the 'otherString' variable in Activity";

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

    public void onPause() {

    }

    public void onResume() {

        mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
    }
}

Can anyone help please ?

6 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

This line:

super.onCreate () {aString};

shouldn't have {aString} in it.

You need to call the onCreate() method of the superclass, making sure to pass in the same parameters that were used for this onCreate() method in this class. (You can pass in savedInstanceState.)

Thanks Ben ... I saw a green bar which says "Correct" :)

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 static String aString;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;

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

        aString = "This variable is set UNKNOWN the 'otherString' variable in Activity";

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

    public void onPause() {

    }

    public void onResume() {

        mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
    }
}

What am i doing wrong?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hi Daetuan,

This is where a real compiler like the one built into Eclipse would help! You would get a syntax error on the line super.onCreate (Bundle saveInstanceState);.

The line above that defines the onCreate() method, saying that it requires one parameter of type Bundle named savedInstanceState (with a 'd'). On your line, you just need to pass in that same parameter. Your line is trying to declare a new Bundle variable named saveInstanceState (without the 'd').

thanks for the help i got it

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 static String aString;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;

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

        aString = "This variable is set UNKNOWN the 'otherString' variable in Activity";

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

    public void onPause() {

    }

    public void onResume() {

        mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
    }
}

What am I doing wrong?

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hi Brian,

Let's take a look at the two lines of your onCreate() method:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate (Bundle savedInstanceState);

The first line defines the function, as well as the variable of type Bundle named 'savedInstanceState' that gets passed in as a parameter. This variable is available for the entire scope of the onCreate() method (until it ends with its closing curly brace).

The 2nd line (the first of your own code) calls the onCreate() method of the superclass, but in passing a parameter you are trying to declare another variable of type Bundle named 'savedInstanceState'. You can only declare a variable once (i.e. specify its data type). So in short, get rid of Bundle in your second line and you'll successfully be passing in the variable that is passed into this version of onCreate(). You're basically passing the same variable around wherever it's needed.

Got it, Thanks.