Question

package com.jonald.mazeonmath;

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


public class MainActivity extends Activity {
MediaPlayer logoMusic;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

MediaPlayer logoMusic = MediaPlayer.create(MainActivity.this,           
R.raw.splash_sound2);
    logoMusic.start();
    Thread logoTimer = new Thread() {
        public void run(){
        try{
                sleep(5000);
    Intent menuIntent = new   Intent("com.jonald.mazeonmath.MENU");
                startActivity(menuIntent);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally{
                finish();
            }
        }
    };
    logoTimer.start();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    logoMusic.release();
}

}

I want to stop the background music after it changes a layout. but when after 5secs it appears to force shutdown the application. But when i remove the logoMusic.release it runs normal but the music continue playing. Can somebody help a newbie like me?

ALSO heres my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jonald.mazeonmath"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.jonald.mazeonmath.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

     <activity
        android:name="com.jonald.mazeonmath.menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.jonald.mazeonmath.MENU" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

</manifest>

Logcat

04-26 20:57:49.149: I/Process(517): Sending signal. PID: 517 SIG: 9
04-26 20:58:01.818: D/dalvikvm(557): GC_EXTERNAL_ALLOC freed 917 objects / 71816 bytes in 80ms
04-26 20:58:07.078: D/AndroidRuntime(557): Shutting down VM
04-26 20:58:07.078: W/dalvikvm(557): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-26 20:58:07.118: E/AndroidRuntime(557): FATAL EXCEPTION: main
04-26 20:58:07.118: E/AndroidRuntime(557): java.lang.RuntimeException: Unable to pause activity {com.jonald.mazeonmath/com.jonald.mazeonmath.MainActivity}: java.lang.NullPointerException
04-26 20:58:07.118: E/AndroidRuntime(557):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3348)
04-26 20:58:07.118: E/AndroidRuntime(557):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3305)
04-26 20:58:07.118: E/AndroidRuntime(557):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3288)
04-26 20:58:07.118: E/AndroidRuntime(557):  at android.app.ActivityThread.access$2500(ActivityThread.java:125)
04-26 20:58:07.118: E/AndroidRuntime(557):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
04-26 20:58:07.118: E/AndroidRuntime(557):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 20:58:07.118: E/AndroidRuntime(557):  at android.os.Looper.loop(Looper.java:123)
04-26 20:58:07.118: E/AndroidRuntime(557):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-26 20:58:07.118: E/AndroidRuntime(557):  at java.lang.reflect.Method.invokeNative(Native Method)
04-26 20:58:07.118: E/AndroidRuntime(557):  at java.lang.reflect.Method.invoke(Method.java:521)
04-26 20:58:07.118: E/AndroidRuntime(557):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-26 20:58:07.118: E/AndroidRuntime(557):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-26 20:58:07.118: E/AndroidRuntime(557):  at dalvik.system.NativeStart.main(Native Method)
04-26 20:58:07.118: E/AndroidRuntime(557): Caused by: java.lang.NullPointerException
04-26 20:58:07.118: E/AndroidRuntime(557):  at com.jonald.mazeonmath.MainActivity.onPause(MainActivity.java:41)
04-26 20:58:07.118: E/AndroidRuntime(557):  at android.app.Activity.performPause(Activity.java:3842)
04-26 20:58:07.118: E/AndroidRuntime(557):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3335)
04-26 20:58:07.118: E/AndroidRuntime(557):  ... 12 more
04-26 20:58:09.619: I/Process(557): Sending signal. PID: 557 SIG: 9
Was it helpful?

Solution

This is because field logoMusic is null in the onPause. Try adding volatile modifier to it. Also the code:

Thread logoTimer = new Thread() {
    public void run() {
    try{
            sleep(5000);
Intent menuIntent = new   Intent("com.jonald.mazeonmath.MENU");
            startActivity(menuIntent);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            finish();
        }
    }
};
logoTimer.start();

doesn't seem right, as I think you should't start new activity from a background thread. Use AsyncTask's onPostExecute [1] for that. Then you shouldn't need the volatile keyword.

1 - http://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute(Result)

EDIT: Please try to replace the code above (starting with Thread logoTimer and ending with logoTimer.start();) with the following code:

new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... params) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        Intent menuIntent = new Intent("com.jonald.mazeonmath.MENU");
        startActivity(menuIntent);
        finish();
    }
}.execute();

And then tell me if this will work.

EDIT 2: It was hidden in the plain sight all the time. Under the line with setContentView, you're declaring method variable, instead setting the field. Remove the MediaPlayer and leave just logoMusic = MediaPlayer.create(....) and it should work.

OTHER TIPS

Probably logo music is null when you try to release it. Try to add a check before invoke release. Something like

if (logoMusic != null) {
   logoMusic.release();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top