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

Error on onResume and onPause

For some reason eclipse is giving me an error on both the onResume and onPause lines, saying that an @ is expected, even though both lines were preceded with the @Override.

The screenshot: http://tinypic.com/r/33eskf5/8

I may have mis-typed something; but I cannot locate any typos.

Thanks!

3 Answers

In the picture you are missing an @Override before onResume. I would add it, and then Clean and Build (located under project at the top of the screen).

Thanks Theodore,

I did try that - I have been adding and removing the @Override statements to see if maybe removing them would help.

I did the clean and build, unfortunately no change.

Here is the error with both Override statements in place. http://tinypic.com/r/ehaerr/8

Do you mind copying and pasting all of the code from MainActivity.java? My next idea would be to delete onResume and let content assist create a new one and see if it is getting the area by default.

Also wondering if maybe you have it inside a set of brackets that it shouldn't be.

Sure,

Thanks again for helping me!

''' Java package com.example.crystalball;

import android.graphics.drawable.AnimationDrawable; import android.hardware.Sensor; import android.hardware.SensorManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.animation.AlphaAnimation; import android.widget.ImageView; import android.widget.TextView;

import com.example.crystalballl.ShakeDetector.OnShakeListener;

public class MainActivity extends ActionBarActivity {

private CrystalBall mCrystalBall = new CrystalBall();
private TextView mAnswerLabel;
private ImageView mCrystalBallImage;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private ShakeDetector mShakeDetector;

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

    // Assign Views from layout file
    mAnswerLabel = (TextView) findViewById(R.id.textView1);
    mCrystalBallImage = (ImageView) findViewById(R.id.imageView1);

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

        @Override
        public void onShake() {
            handleNewAnswer();

        };
    });
  @Override
  public void onResume() {
      super.onResume();
      mSensorManager.registerListener(mShakeDetector, mAccelerometer, 
              SensorManager.SENSOR_DELAY_UI);
  }


 @Override
 public void onPause() {
    super.onPause();  
    mSensorManager.unregisterListener(mShakeDetector);
  }
}

private void animateCrystalBall() {

    mCrystalBallImage.setImageResource(R.drawable.ball_animation);
    AnimationDrawable ballAnimation = (AnimationDrawable) mCrystalBallImage.getDrawable();
    if (ballAnimation.isRunning()){
        ballAnimation.stop();
    };
    ballAnimation.start();
}

private void animateAnswer() {
    AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
    fadeInAnimation.setDuration(1500);
    fadeInAnimation.setFillAfter(true);
    mAnswerLabel.setAnimation(fadeInAnimation);
}

private void playSound() {
    MediaPlayer player = MediaPlayer.create(this, R.raw.crystal_ball);
    player.start();
    player.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void handleNewAnswer() {
    String answer = mCrystalBall.getAnAnswer();
    //Update the label with our dynamic answer
    mAnswerLabel.setText(answer);

    animateCrystalBall();
    animateAnswer();
    playSound();
}

} '''