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
Kunal Shukla
1,300 PointsShaking Things Up - Activity Cycle, problem with initializing variable
Hi, so my app is not working right now. I am moving on with the challenges because of time constraints.
The code with Toast is where I have seen error messages, and also the code with this line mAnswerLabel.setText(answer); has had an error message as well. If you see any other errors in my code, you would be a great help. I will keep updating my code until I am finished with the Build a Simple Android App process. Hope the information I have given you helps!
package com.example.crystall.ball;
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 android.widget.Toast;
import com.example.crystall.ball.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);
//Declare our View variables and Assign them 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();
Toast welcomeToast = Toast.makeText(this, "Look at me up here!", Toast.LENGTH_LONG);
welcomeToast.setGravity(Gravity.TOP, 0, 0);
welcomeToast.show();
@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() {
public void onCompletion(MediaPlayer mp) {
mp.release();
// TODO Auto-generated method stub
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void handleNewAnswer() {
String Answer = mCrystalBall.getAnAnswer();
CharSequence answer = Answer;
//Update the label with our dynamic answer
mAnswerLabel.setText(answer);
animateCrystalBall();
animateAnswer();
playSound();
}
}
1 Answer
Ben Jakuben
Treehouse TeacherFor the answer variable, you don't need this line:
CharSequence answer = Answer;
Strings can be used where CharSequence is expected because CharSequence is just an interface, not a full-fledged data type. Basically, Strings implement the CharSequence interface (other data types do, too). You need to use a full-fledged data type to create a variable.
Remove that line and change Answer to answer in your String declaration and that error should go away.