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

App crash upon opening

So the title basically says it all and I am not sure why it's doing this. Here is the code for the MainActivity.java where I believe the problem to consist. Be aware that i have renamed the answer generator to advisor,java so there is no problems with that.

package com.sweet.sweeter;


import android.*;
import android.app.*;
import android.graphics.drawable.*;
import android.hardware.*;
import android.media.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.view.animation.*;
import android.widget.*;
import com.sweet.sweeter.ShakeDetector.*;


public class MainActivity extends Activity
{


    public static final String TAG = MainActivity.class.getSimpleName ();
    private Advisor mAdvisor = new Advisor ();
    private TextView mAnswerLabel;
    private ImageView mCrystalBallImage;
    private MediaPlayer mPlayer;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;


    @Override
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.main);

        // assigning the Views from the layout file
        mAnswerLabel = (TextView) findViewById (R.id.answer_textView);

        mCrystalBallImage = (ImageView)findViewById (R.id.imageView);
        //Setting up the SensorManager
        mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor (Sensor.TYPE_ACCELEROMETER);
        mShakeDetector = new ShakeDetector (new OnShakeListener (){


                public void onShake ()
                {
                    handleNewAnswer ();
                }
            });

        /*Toast.makeText(this,toastMessage, Toast.LENGTH_LONG).show();

         Toast welcomeToast = Toast.makeText(this,"Look at me way up here!", 
         Toast.LENGTH_LONG);
         welcomeToast.setGravity(Gravity.TOP,0,0);
         welcomeToast.show();*/
        Log.d (TAG, "onCreate()");
    }

    @Override
    public void onResume ()
    {   super.onResume ();
        mSensorManager.registerListener (mShakeDetector, mAccelerometer, 
                                         SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    public void onPause ()
    {   super.onPause ();
        mSensorManager.unregisterListener (mShakeDetector);
        mPlayer.release ();
    }


    private void handleNewAnswer ()
    {

        String answer = mAdvisor.getAnAnswer ();

        //Update the label with our dynamic answer
        mAnswerLabel.setText (answer);

        animateCrystalBall ();
        animateAnswer ();
        playSound ();
    }

    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 ()
    {
        mPlayer.create (this, R.raw.crystal_ball_sound);
        mPlayer.start ();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
}

Please help me!

Cameron Stewart
Cameron Stewart
18,050 Points

Do you need to import the toast widget?

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

On an app crash you should get some kind of error message in the Console and/or Logcat view at the bottom of Eclipse. Can you paste in any error messages that you see down there? (Sorry for the late reply - I missed this one somehow.)