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
Rodrigo Muñoz
Courses Plus Student 20,171 PointsSimple Android App > Activity Lifecycle
Well I've being coding the same as in the course does unless the package name and I came with this problem about using the Shake Detector when I have to code the onPause and onResume method.
@Override
public void onResume(){
super.onResume();
mSensorManager.registerListener(mShakeDetector, mAccelerometer,
SensorManager.SENSOR_DELAY_UI);
}
@Override
public void onPause(){
super.onPause();
mSensorManager.unregisterListener(mShakeDetector);
}
}
The public void shows an error and the values inside the mSensorManage.registerListener() no longer shows in blue as they should. I thought the version of the language may be the one causing this problem but I'm not sure at all. Help me fixing this please.
6 Answers
Ernest Grzybowski
Treehouse Project ReviewerHey Rodrigo,
I'm trying to narrow down your problem, but I'm not really sure in what context you are asking about the code.
Are you having trouble on the original project or are you having trouble implementing the shake detector in another app?
Make sure to drag the ShakeDetector.java file into eclipse. As seen from 2:40 to 3:05 in Adding a Shake Detector.
Finally, because I'm finding it a little hard to understand your problem can you post your ENTIRE code?
Rodrigo Muñoz
Courses Plus Student 20,171 PointsI am having troubles with the original project but I only change the package name from com.treehouse.crystalball to com.trollglodita.crystalballs so I had to modify the ShakeDetecor.java file changing the package name. All other code is exactly as it is in the course. But my doubt came in the next video after adding the ShakeDetector file, in the activity lifecycle there is a modification of the code using @override and then public void onResume(){... Well that is the thing causing troubles and I don't know why, anyway, here is the code from the mainActivity file
package com.trollglodita.crystalballs;
import android.app.Activity;
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.util.Log;
import android.view.Menu;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import com.trollglodita.crystalballs.ShakeDetector.OnShakeListener;
public class MainActivity extends Activity {
public static final String TAG = MainActivity.class.getSimpleName();
private CrystalBall mCrystalBall = new CrystalBall();
private TextView mAnswerLabel;
private ImageView mCrystalBallImage;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private ShakeDetector mShakeDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Assign the Views from the 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();
}
});
// Toast.makeText(this, "Yay! Our activity was created", Toast.LENGTH_LONG).show();
Log.d(TAG, "We're logging from the onCreate() method!");
@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;
}
private void handleNewAnswer() {
String answer = mCrystalBall.getAnAnswer();
// Update the label with our dynamic answer
mAnswerLabel.setText(answer);
animateCrystalBall();
animateAnswer();
playSound();
}
}
I believe is something to do with the super class method when using onResume and onPause
Ben Jakuben
Treehouse TeacherI think this may be an issue others have had with weird project-specific Java compiler settings. Check out this other post to see if it helps:
https://teamtreehouse.com/forum/adding-a-shake-detector#show-comment-8492
Rodrigo Muñoz
Courses Plus Student 20,171 PointsI've tried this using the 1.5 , 1.6 and 1.7 compiler version but still can't fix the error. S.O.S
Ben Jakuben
Treehouse TeacherAh, I should have looked more closely at the code you posted in here. Check out where you're declaring onPause() and onResume(). You have them nested inside the onCreate() method, which is illegal. Just cut and paste them outside of that method and it works!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Assign the Views from the 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();
}
});
// Toast.makeText(this, "Yay! Our activity was created", Toast.LENGTH_LONG).show();
Log.d(TAG, "We're logging from the onCreate() method!");
}
@Override
public void onResume(){
super.onResume();
mSensorManager.registerListener(mShakeDetector, mAccelerometer,
SensorManager.SENSOR_DELAY_UI);
}
@Override
public void onPause(){
super.onPause();
mSensorManager.unregisterListener(mShakeDetector);
}
Rodrigo Muñoz
Courses Plus Student 20,171 PointsThis really helped. Thanks guys, I could never keep on going without your help