java.lang.StackOverflowError:Android Studio上的堆栈大小为8MB。怎么解决呢?

时间:2019-04-03 07:35:39

标签: java android

当我在android studio中运行的应用程序遇到此问题时。

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)

我已经检查了第48行的代码是

    mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

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

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

我不知道该如何解决这个问题,因此我不得不启用高级配置文件,因为我指的是这个网站。但它不起作用=(

InputConnectionWrapper.commitText StackOverflowError in HTC devices

下面的代码是我的音乐服务代码

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;
    }
}

1 个答案:

答案 0 :(得分:0)

问题:您正在调用错误的onError方法。您必须改为调用onError中的MusicService

解决方案:从以下位置更改代码

mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

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

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;
    }
});