Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping/releasing Mediaplayer in onDestory() causes NullPointerException

I'm trying to stop and release a MediaPlayer object in onDestroy(), but the app keeps crashing. Here is the code:

void StartSound(final String ReaderPath) {
    mediaplayer = MediaPlayer.create(test2.this, Uri.parse(ReaderPath));

    try {
        mediaplayer.start();
        mediaplayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                current++;
                try {
                    mp.reset();
                    mp.setDataSource(ReaderPath);
                    mp.prepare();

                } catch (Exception e) {
                    e.printStackTrace();
                }
                mp.start();

            }
        });

    } catch (Exception e) {

    }
}

@Override
    protected void onPause() {
        if (mediaplayer != null && mediaplayer.isPlaying())
            mediaplayer.pause();
        super.onPause();

    }

    @Override
    protected void onStop() {
        if (mediaplayer != null && mediaplayer.isPlaying()) {
            mediaplayer.release();
            mediaplayer.stop();
        }
        super.onDestroy();

    }

Logcat:

08-21 22:52:26.512: E/AndroidRuntime(16410): java.lang.RuntimeException: Unable to destroy activity {com.example.test/com.example.test.test2}: java.lang.NullPointerException
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3235)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3253)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.ActivityThread.access$1200(ActivityThread.java:132)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1190)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.os.Looper.loop(Looper.java:137)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.ActivityThread.main(ActivityThread.java:4575)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at java.lang.reflect.Method.invokeNative(Native Method)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at java.lang.reflect.Method.invoke(Method.java:511)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at dalvik.system.NativeStart.main(Native Method)
08-21 22:52:26.512: E/AndroidRuntime(16410): Caused by: java.lang.NullPointerException
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1406)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1876)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:316)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.Activity.performDestroy(Activity.java:4629)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1079)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3222)
08-21 22:52:26.512: E/AndroidRuntime(16410):    ... 11 more

Why is this?

like image 704
Omar Avatar asked Aug 21 '12 19:08

Omar


1 Answers

Why are you calling super.onDestroy() from your onStop() method? You should be calling super.onStop(). Or is that a typing error?

The answer is probably that you are calling stop() on the MediaPlayer after you've called release(). Once you've called release() the MediaPlayer is invalidated and no longer available.

like image 90
Squonk Avatar answered Sep 18 '22 13:09

Squonk