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

I am stuck on the Shake detector

I get these warnings, by number 2, I guess it misses a String answer see below;

  1. THE VALUE OF THE FIELD IS NOT USED;
private Crystalball mCrystalball = new Crystalball();
private Sensor mAccelerometer;
private ShakeDetector mShakeDetector;
  1. ANSWER CANNOT BE RESOLVED TO A VARIABLE ( is mAnswerLabel.setText(answer); Code;
package com.example.crystalball;

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.view.Menu;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.crystalball.ShakeDetector.OnShakeListener;

public class MainActivity extends Activity {
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);

        //Asign 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();


                // TODO Auto-generated method stub
            }           
    });
        }


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);//1500Ml is 1,5 seconde
        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() {
    mAnswerLabel.setText(answer);
        animateCrystalBall();
        animateAnswer();
        playSound();
}
}

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

So close! That 2nd error message gives you a clue. The line

mAnswerLabel.setText(answer); says that answer cannot be resolved to a variable. Based on that, we go looking for the declaration of the answer variable. There isn't one! That's the error. You are missing the following line that should be right before that line:

String answer = mCrystalBall.getAnAnswer();

That also explains why you get the warning 'value of the field is not used'. That's about mCrystalBall, which is only used in this line that's missing.