-1

I have been following some android tutorials on the web. I tried copying his code to the best of my ability but whenever I run the application it crashes. I looked through the console and found it was a Nullpointer exception that caused it. The application is meant to play a sound and then close and then go to another layout. Here is my code, please tell me what I did wrong.

    package com.greg.hello;

    import android.app.Activity;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.Bundle;

    public class MainActivity extends Activity {
    MediaPlayer mpSplash;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        MediaPlayer mpSplash = MediaPlayer.create(this, R.raw.explosion);
        mpSplash.start();
        Thread logoTimer = new Thread(){
            public void run(){
                try{
                    int logoTimer=0;
                    while(logoTimer<8000){
                        sleep(100);
                        logoTimer=logoTimer+100;
                    }
                    startActivity(new     Intent("com.greg.hello.CLEARSCREEN"));
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    setContentView(R.layout.tutorial1);
                    e.printStackTrace();
                }

                finally{
                    finish();
                }
            }   
        };
        logoTimer.start();
        finish();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        mpSplash.release();
        startActivity(new Intent("com.greg.hello.TUTORIALONE"));
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        mpSplash.pause();
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mpSplash.start();
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        setContentView(R.layout.activity_main);
    }

}
Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
General Stubbs
  • 173
  • 2
  • 9

2 Answers2

2

This definitely one NPE, replace

MediaPlayer mpSplash = MediaPlayer.create(this, R.raw.explosion);

with

this.mpSplash = MediaPlayer.create(this, R.raw.explosion);
home
  • 12,468
  • 5
  • 46
  • 54
0

inside the onCreate() with the line

MediaPlayer mpSplash = MediaPlayer.create(this, R.raw.explosion);

is a local variable.

replace it with

this.mpSplash = MediaPlayer.create(this, R.raw.explosion);

* when NullPointerException occured.

in onDestroy()

You called

mpSlash.release()

which is not yet initialized but you thought it is initialized in onCreate()