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

Bahi Hussien
PLUS
Bahi Hussien
Courses Plus Student 13,128 Points

Emulator was working fine / Now showing Black Screen after adding the Shaking xml

Emulator was working fine / Now showing Black Screen after adding the Shake Xml

When i delete the Shake Code it works ...

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

    @Override
    public void onShake() {
        answerHandle();
    }
});

Full Code

package com.example.crystallball;

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.crystallball.ShakeDetector.OnShakeListener;

    public class MainActivity extends Activity {
    private CrystallBallGenerator mcrystall = new CrystallBallGenerator() ;
    private TextView mAnswerlabel;
    private ImageView mCrystallballimage;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;





    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAnswerlabel = (TextView) findViewById(R.id.textView1);
        mCrystallballimage = ( 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() {
            answerHandle();
        }
    });


      Toast.makeText(this, "welcome",Toast.LENGTH_LONG).show();
    }







    private void animateCrystallBall() {

        mCrystallballimage.setImageResource(R.drawable.ball_animation);
        AnimationDrawable ballAnimation = ( AnimationDrawable ) mCrystallballimage.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();
        }
    });
    }


    protected TextView setText(String string) {

        return null;
    }


    @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 answerHandle() {
        String answer = mcrystall.getananswer();
        mAnswerlabel.setText(answer);
        animateCrystallBall();
        animateAnswer();
        playsound();
    }

      }

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Can you tell us a little more about what kind of error you are experiencing? Do you have any errors in Eclipse, or does something happen when you run it in the emulator? The ShakeDetector code will only work on a device as there is no way to simulate a shake event in the emulator. If you'd like you can also zip up your project and send it to help@teamtreehouse.com for us to take a look.