Search code examples
androidandroid-layoutadmobads

App crashing after coming back from banner ads


When I click on banner ads, it is opening fine but when i come back to app it is forcing app to crash and close. It is a soundboard app.

What should i add to the application so it does not crash when activity is resumed after closing or coming back to the app.

Just a beginner so any help would be appreciated.

Activity1

public class Activity1 extends Activity {
    int selectedSoundId;
    MediaPlayer player;

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

        AdView mAdView = (AdView) findViewById(R.id.adMob);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        player = new MediaPlayer();
        final Resources res = getResources();

        final int[] buttonIds = { };
        final int[] soundIds = { };

        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                for (int i = 0; i < buttonIds.length; i++) {
                    if (v.getId() == buttonIds[i]) {
                        selectedSoundId = soundIds[i];
                        AssetFileDescriptor afd = res
                                .openRawResourceFd(soundIds[i]);
                        player.reset();
                        try {
                            player.setDataSource(afd.getFileDescriptor(),
                                    afd.getStartOffset(), afd.getLength());
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            player.prepare();
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        player.start();
                        break;
                    }
                }
            }
        };

        for (int i = 0; i < buttonIds.length; i++) {
            Button soundButton = (Button) findViewById(buttonIds[i]);
            registerForContextMenu(soundButton);
            soundButton.setOnClickListener(listener);
        }
    }

    protected void onPause() {
        super.onPause();
        player.stop();
        player.release();
        player.pause();

    }

}

Activity1.xml

<RelativeLayout>
    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adMob"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/>
</RelativeLayout>

Crash Logs:

FATAL EXCEPTION: main Process: com.example.keshav.giantsoundboard, PID: 5366 java.lang.RuntimeException: Unable to pause activity {com.example.keshav.giantsoundboard/com.example.keshav.giantsoundboard.Activity3}: java.lang.IllegalStateException at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3381) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3340) at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3315) at android.app.ActivityThread.-wrap13(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.IllegalStateException at android.media.MediaPlayer._stop(Native Method) at android.media.MediaPlayer.stop(MediaPlayer.java:1231) at com.example.keshav.giantsoundboard.Activity3.onPause(Activity3.java:95) at android.app.Activity.performPause(Activity.java:6348) at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1311) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3367) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3340)  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3315)  at android.app.ActivityThread.-wrap13(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


Solution

  • After release(), the MediaPlayer object is no longer available.

    Inside onPause() method of activity, You're calling pause() method on MediaPlayer object after you released that player.

    so remove player.release();