1

As my application i run in android studio got this problem.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.user.brain_storming, PID: 11742
    java.lang.StackOverflowError: stack size 8MB
        at com.example.user.brain_storming.MusicService$1.onError(MusicService.java:48)
        at com.example.user.brain_storming.MusicService$1.onError(MusicService.java:48)

And i have check the code the line 48 is this

    mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

        public boolean onError(MediaPlayer mp, int what, int
                extra) {

            onError(mPlayer, what, extra);
            return true;
        }
    });

I dun know how to solve it and i had go to enable the advanced profiling as i refer to this website. But its not work =(

InputConnectionWrapper.commitText StackOverflowError in HTC devices

The code below is my music service code

public class MusicService extends Service implements MediaPlayer.OnErrorListener {

    private final IBinder mBinder = new ServiceBinder();
    MediaPlayer mPlayer;
    private int length = 0;

    public MusicService() {
    }

    public class ServiceBinder extends Binder {
        MusicService getService() {
            return MusicService.this;
        }
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mPlayer = MediaPlayer.create(this, R.raw.play);
        mPlayer.setOnErrorListener(this);

        if (mPlayer != null) {
            mPlayer.setLooping(true);
            mPlayer.setVolume(50, 50);
        }


        /*mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

            public boolean onError(MediaPlayer mp, int what, int
                    extra) {

                onError(mPlayer, what, extra);
                return true;
            }
        });*/
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (mPlayer != null) {
            mPlayer.start();
        }
        return START_NOT_STICKY;
    }

    public void pauseMusic() {
        if (mPlayer != null) {
            if (mPlayer.isPlaying()) {
                mPlayer.pause();
                length = mPlayer.getCurrentPosition();
            }
        }
    }

    public void resumeMusic() {
        if (mPlayer != null) {
            if (!mPlayer.isPlaying()) {
                mPlayer.seekTo(length);
                mPlayer.start();
            }
        }
    }

    public void startMusic() {
        mPlayer = MediaPlayer.create(this, R.raw.play);
        mPlayer.setOnErrorListener(this);

        if (mPlayer != null) {
            mPlayer.setLooping(true);
            mPlayer.setVolume(50, 50);
            mPlayer.start();
        }

    }

    public void stopMusic() {
        if (mPlayer != null) {
            mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPlayer != null) {
            try {
                mPlayer.stop();
                mPlayer.release();
            } finally {
                mPlayer = null;
            }
        }
    }

    public boolean onError(MediaPlayer mp, int what, int extra) {

        Toast.makeText(this, "Music player failed", Toast.LENGTH_SHORT).show();
        if (mPlayer != null) {
            try {
                mPlayer.stop();
                mPlayer.release();
            } finally {
                mPlayer = null;
            }
        }
        return false;
    }
}
Steven Yu
  • 135
  • 1
  • 12

1 Answers1

1

Problem: You are calling wrong onError method. You must calling onError of MusicService instead.

Solution: Change your code from

mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

    public boolean onError(MediaPlayer mp, int what, int
            extra) {
        onError(mPlayer, what, extra);
        return true;
    }
});

to

mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

    public boolean onError(MediaPlayer mp, int what, int
            extra) {
        // Calling onError of MusicService instead this onError method.
        MusicService.this.onError(mPlayer, what, extra);
        return true;
    }
});
Son Truong
  • 13,661
  • 5
  • 32
  • 58