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

Confused on "shake" and now it won't run.

Okay guys and gals. I have played around with this code until I think I have totally broken it - lol. Does anyone have the completed Android crystal ball code they would be willing to post so I can compare mine to theirs? I commented out all the "shake" stuff but it still fails to run. HELP! I am very new to programming so I would appreciate the help.

Activity_main.xml

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ball01" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignLeft="@+id/imageView1"
android:text="Enlighten Me!"
android:textColor="#3f0f7f"
android:textSize="24sp"
android:textStyle="bold|italic"
android:typeface="serif" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:weightSum="1" >
<View
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.2" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:gravity="center_horizontal"
android:shadowColor="@android:color/white"
android:shadowRadius="10"
android:textColorLink="@android:color/white"
android:textSize="32sp" />
<View
android:id="@+id/View2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.2" />
</LinearLayout>
</RelativeLayout>

MainActivity.java

package com.example.crystal.ball;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
publicclass MainActivity extends Activity {
private CrystalBall mCrystalBall = new CrystalBall();
private TextView mAnswerLabel;
private Button mGgetAnswerButton;
private ImageView mCrystalBallImage;
//private SensorManager mSensorManager;
//private Sensor mAccelerometer;
//private ShakeDetector mShakeDetector;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Assign the Views from the layout file
mAnswerLabel = (TextView) findViewById(R.id.textView1);
mGgetAnswerButton = (Button) findViewById(R.id.button1);
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);
}
{
mGgetAnswerButton.setOnClickListener(new View.OnClickListener() {
publicvoid onClick(View v) {
String answer = mCrystalBall.getAnAnswer();
//Update the label with our dynamic answer
mAnswerLabel.setText(answer);
animateCrystalBall();
animateAnswer();
playSound();
}
});
}
publicvoid onClick(View v) {
handleNewAnswer();
}
privatevoid handleNewAnswer() {
// TODO Auto-generated method stub
}
privatevoid animateCrystalBall() {
mCrystalBallImage.setImageResource(R.drawable.ball_animation);
AnimationDrawable ballAnimation = (AnimationDrawable) mCrystalBallImage.getDrawable();
if (ballAnimation.isRunning()) {
ballAnimation.stop();
}
ballAnimation.start();
}
privatevoid animateAnswer() {
AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
fadeInAnimation.setDuration(1500);
fadeInAnimation.setFillAfter(true);
mAnswerLabel.setAnimation(fadeInAnimation);
}
privatevoid playSound() {
MediaPlayer player = MediaPlayer.create(this, R.raw.crystal_ball);
player.start();
player.setOnCompletionListener(new OnCompletionListener() {
@Override
publicvoid onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
}

Crystalball.java

package com.example.crystal.ball;
import java.util.Random;
publicclass CrystalBall {
//Member variables (properties about the object)
public String[] mAnswers = {
"It is certain",
"It is decidedly so",
"All signs say YES",
"The stars are not aligned",
"My reply is no",
"It is doubtful",
"Better not tell you now",
"Concentrate and ask again",
"Unable to answer now",
"It is hard to say" };
//Methods (abilities: things the object can do)
public String getAnAnswer() {
String answer = "";
//Randomly select one of three answers: Yes, No, or Maybe
Random randomGenerator = new Random(); // Construct a new Random number generator
int randomNumber = randomGenerator.nextInt(mAnswers.length);
answer = mAnswers[randomNumber];
return answer;
}
}

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

This is usually a problem importing the files for some reason. Check out this thread: https://teamtreehouse.com/forum/adding-a-shake-detector

Also, if you want to compare with our project code, check out the "Project Files" link on each video page. It links to either the code at the end of the current video or current stage, depending on how good I was at saving my code after each video. :)