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

*Crystal Ball Misc Errors* Cont.

I've been talking to Ben Jakuben For a while troubleshooting my errors but I just can't win lol. I got my project clear of errors but now it wont run it crashes every time. Here is my mainactivity.java

package com.example.crystalball;

import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    private CrystalBall mCrystalBall = new CrystalBall();
    private TextView mAnswerLabel;
    private Button mGetAnswerButton;
    private ImageView mCrystalBallImage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Assign the Views from the layout file
        mAnswerLabel = (TextView) findViewById(R.id.textView1);
        mGetAnswerButton= (Button) findViewById(R.id.button1);
        mCrystalBallImage.setImageResource(R.drawable.ball_animation);

        mGetAnswerButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String answer = mCrystalBall.getAnAnswer();

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

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

    private void animateCrystalBall() {
        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();
            }
        });
    }

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

This is what it says on logcat if I try to run my app
http://gyazo.com/4221f83ff73166352a2f85ff99a01b57
Guillaume Maka
Guillaume Maka
Courses Plus Student 10,224 Points

Can you zip the project and send it to me so I can run it and give you an appropriate answer !

1 Answer

Guillaume Maka
PLUS
Guillaume Maka
Courses Plus Student 10,224 Points

You get a NullPointerException:

05-19 20:27:44.132: E/AndroidRuntime(1619): Caused by: java.lang.NullPointerException
05-19 20:27:44.132: E/AndroidRuntime(1619):     at com.example.crystalball.MainActivity.onCreate(MainActivity.java:30)

Add

mCrystalBallImage = (ImageView) findViewById(R.id.imageView1);

before

mCrystalBallImage.setImageResource(R.drawable.ball_animation);

You just forgot to initialize ""mCrystalBallImage"" variable.

http://gyazo.com/1525ebe7699b61c85e2fab34866ebace

thanks a bunch man!

I owe ya one

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Thanks for following along and helping, Guillaume Maka!

Just finished up my crystal ball app! Thanks Ben and Guillaume

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Did you submit it? Go put an app on Google Play! :)

Well doing the toast notification lesson now, so I guess not 100% done but i'm superrrr close haha