如何解决java.lang.StackOverflowError?

0 人关注

我在运行应用程序时得到了java.lang.StackOverflowError错误。我想我用了太多的项目还是什么? 这里是错误的地方(看起来是无穷大)。

    Process: com.ashtim.yulduzlari, PID: 14548
    java.lang.StackOverflowError
        at java.util.HashMap.doubleCapacity(HashMap.java:589)
        at java.util.HashMap.put(HashMap.java:419)
        at androidx.arch.core.internal.FastSafeIterableMap.putIfAbsent(FastSafeIterableMap.java:50)
        at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:175)
        at androidx.activity.ComponentActivity.<init>(ComponentActivity.java:230)
        at androidx.fragment.app.FragmentActivity.<init>(FragmentActivity.java:103)
        at androidx.appcompat.app.AppCompatActivity.<init>(AppCompatActivity.java:94)
        at com.ashtim.yulduzlari.MainActivity.<init>(MainActivity.java:14)
        at com.ashtim.yulduzlari.SongActivity.<init>(SongActivity.java:23)
        at com.ashtim.yulduzlari.MainActivity.<init>(MainActivity.java:16)
        at com.ashtim.yulduzlari.SongActivity.<init>(SongActivity.java:23)
        at com.ashtim.yulduzlari.MainActivity.<init>(MainActivity.java:16)
        . . . . . . . . . . . . . . . A lot of the same error . . . . . .
        at com.ashtim.yulduzlari.SongActivity.<init>(SongActivity.java:23)
        at com.ashtim.yulduzlari.MainActivity.<init>(MainActivity.java:16)
        at com.ashtim.yulduzlari.SongActivity.<init>(SongActivity.java:23)
        at com.ashtim.yulduzlari.MainActivity.<init>(MainActivity.ja
2021-08-16 16:37:12.496 26798-26798/? E/AppsCustomizePagedView: info.activityInfo.name = com.keniu.security.main.AddDesktopShortcutActivity

这里是我的MainActivity.java文件。 第16行:最终MediaPlayer mp = new SongActivity().getMp()。

package com.ashtim.yulduzlari;
public class MainActivity extends AppCompatActivity {
    final MediaPlayer mp = new SongActivity().getMp();
    List<Song> songs = new ArrayList<>();
    Song playingSong;
    RecyclerView recyclerView;
    SongAdapter adapter;
    public static boolean isPlaying, repeat, repeatOne, shuffle;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initSongs();
        initBooleans();
        recyclerView = findViewById(R.id.recyclerView);
        adapter = new SongAdapter(songs);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter.setOnItemClickListener(position -> {
            play(songs.get(position));
            isPlaying = true;
    void initSongs () {
    void initBooleans () {
        isPlaying = false;
        repeat = false;
        repeatOne = false;
        shuffle = false;
    public void setPlayingSong (Song playingSong) {
        this.playingSong = playingSong;
    public Song getPlayingSong () {
        return playingSong;
    public List<Song> getSongs () {
        return songs;
    void play(Song s){
        playingSong = s;
        Intent i = new Intent(this, SongActivity.class);
        i.putExtra("img", s.getImgId());
        i.putExtra("name", s.getName());
        i.putExtra("artist", s.getArtist());
        i.putExtra("duration", s.getDuration());
        i.putExtra("song", s.getMusicId());
        startActivity(i);

这里是我的SongActivity.java文件。 第23行: MainActivity ma = new MainActivity();

package com.ashtim.yulduzlari;
public class SongActivity extends AppCompatActivity {
    MainActivity ma = new MainActivity();
    MediaPlayer mp;
    List<Song> songs;
    String name, artist, duration;
    int songId, imgId;
    boolean repeatOff, repeatList, repeatOne, shuffleSongs;
    SeekBar seekBar;
    TextView textName, textArtist, textDuration, textProgressTime;
    ImageView imgArtisToolbar, repeat, previous, play, next, shuffle;
    CircleImageView imgArtistMain;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_song);
        getData();
        initViews();
        setViews();
        setBooleans();
    void play (int position) {
        mp = MediaPlayer.create(this, songs.get(position).getMusicId());
        shuffle.setOnClickListener(v -> {
            shuffleSongs = ! shuffleSongs;
            if (repeatOne = true) {
                repeatOne = false;
                repeatOff = true;
                repeat.setImageResource(R.drawable.repeat_off);
            } else if (repeatList) {
                repeatList = false;
                repeatOff = true;
                repeat.setImageResource(R.drawable.repeat_off);
        repeat.setOnClickListener(v -> {
            if (shuffleSongs = false) {
                if (repeatOff) {
                    repeatList = true;
                    repeatOff = repeatOne = false;
                    mp.setLooping(false);
                    repeat.setImageResource(R.drawable.repeat_on);
                } else if (repeatList) {
                    repeatOne = true;
                    repeatList = repeatOff = false;
                    repeat.setImageResource(R.drawable.repeat_one);
                } else if (repeatOne) {
                    repeatOff = true;
                    repeatOne = repeatList = false;
                    mp.setLooping(true);
                    repeat.setImageResource(R.drawable.repeat_off);
            } else {
                Toast.makeText(this, "\"Aralashtirish\"ni o'chiring", Toast.LENGTH_SHORT).show();
        mp.setOnCompletionListener(mp -> {
            if (!mp.isLooping() && songs.get(position + 1) != null) {
                mp = MediaPlayer.create(this, songs.get(position + 1).getMusicId());
                ma.setPlayingSong(songs.get(position + 1));
                textName.setText(ma.getPlayingSong().getName());
                textArtist.setText(ma.getPlayingSong().getArtist());
                textDuration.setText(ma.getPlayingSong().getDuration());
                imgArtisToolbar.setImageResource(ma.getPlayingSong().getImgId());
                imgArtistMain.setImageResource(ma.getPlayingSong().getImgId());
                mp.start();
            } else if (shuffleSongs == true){
                Random r = new Random();
                int randomPos = r.nextInt(songs.size() - 1);
                mp = MediaPlayer.create(this, songs.get(randomPos).getMusicId());
                ma.setPlayingSong(songs.get(randomPos));
                textName.setText(ma.getPlayingSong().getName());
                textArtist.setText(ma.getPlayingSong().getArtist());
                textDuration.setText(ma.getPlayingSong().getDuration());
                imgArtisToolbar.setImageResource(ma.getPlayingSong().getImgId());
                imgArtistMain.setImageResource(ma.getPlayingSong().getImgId());
                mp.start();
            } else if (songs.get(position + 1) == null) {
                if (repeatList == true) {
                    mp = MediaPlayer.create(this, songs.get(0).getMusicId());
                    ma.setPlayingSong(songs.get(0));
                    textName.setText(ma.getPlayingSong().getName());
                    textArtist.setText(ma.getPlayingSong().getArtist());
                    textDuration.setText(ma.getPlayingSong().getDuration());
                    imgArtisToolbar.setImageResource(ma.getPlayingSong().getImgId());
                    imgArtistMain.setImageResource(ma.getPlayingSong().getImgId());
                    mp.start();
        previous.setOnClickListener(v -> playPrev(this, songs, mp, ma, position - 1));
        playPause(mp);
        next.setOnClickListener(v -> playNext(this, songs, mp, ma, position + 1));
        setSeekBarProgress();
        if (mp != null) {
            mp.start();
    void playPrev (Context c, List<Song> songs, MediaPlayer mp, MainActivity ma, int prevSongPos) {
        if (prevSongPos == - 1) {
            Toast.makeText(c, "Birinchi musiqa", Toast.LENGTH_SHORT).show();
        } else {
            if (mp != null && ! (mp.getCurrentPosition() > 10000)) {
                mp.stop();
                mp.release();
                mp = MediaPlayer.create(this, songs.get(prevSongPos).getMusicId());
                ma.setPlayingSong(songs.get(prevSongPos));
                setSeekBarProgress();
                mp.start();
            } else if (mp != null) {
                mp.seekTo(0);
    public void playNext (Context c, List<Song> songs, MediaPlayer mp, MainActivity ma, int nextSongPos) {
        if (nextSongPos == songs.size() + 1) {
            Toast.makeText(c, "Oxirgi musiqa", Toast.LENGTH_SHORT).show();
        } else {
            if (mp != null) {
                mp.stop();
                mp.release();
                mp = MediaPlayer.create(this, songs.get(nextSongPos).getMusicId());
                ma.setPlayingSong(songs.get(nextSongPos));
                setSeekBarProgress();
                mp.start();
    public void playPause (MediaPlayer mp) {
        if (mp != null && mp.isPlaying()) {
            mp.stop();
            play.setImageResource(R.drawable.play);
        } else {
            if (mp != null) {
                mp.start();
                play.setImageResource(R.drawable.pause);
    void setSeekBarProgress () {
        seekBar.setMax(mp.getDuration() / 1000);
        Timer t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run () {
                if (mp != null) {
                    int currentTime = mp.getCurrentPosition() / 1000;
                    seekBar.setProgress(currentTime);
        }, 0, 1000);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {
                if (mp != null && fromUser) {
                    mp.seekTo(progress * 1000);
                    seekBar.setProgress(progress * 1000);
            @Override
            public void onStartTrackingTouch (SeekBar seekBar) {
            @Override
            public void onStopTrackingTouch (SeekBar seekBar) {
    void initViews () {
        seekBar = findViewById(R.id.seekbar);
        textName = findViewById(R.id.songName);
        textArtist = findViewById(R.id.artistName);
        textProgressTime = findViewById(R.id.progress_time);
        textDuration = findViewById(R.id.duration);
        imgArtisToolbar = findViewById(R.id.img_activity_toolbar);
        repeat = findViewById(R.id.repeat);
        previous = findViewById(R.id.previous);
        play = findViewById(R.id.play);
        next = findViewById(R.id.next);
        shuffle = findViewById(R.id.shuffle);
        imgArtistMain = findViewById(R.id.img_activity);
    void setViews () {
        songs = ma.getSongs();
        textName.setText(name);
        textArtist.setText(artist);
        textDuration.setText(duration);
        imgArtisToolbar.setImageResource(imgId);
        imgArtistMain.setImageResource(imgId);
    void setBooleans () {
        repeatOff = true;
        repeatList = false;
        repeatOne = false;
        shuffleSongs = false;
    void getData () {
        Intent i = getIntent();
        name = i.getStringExtra("name");
        artist = i.getStringExtra("artist");
        duration = i.getStringExtra("duration");
        songId = i.getIntExtra("song", 0);
        imgId = i.getIntExtra("img", 0);
    public MediaPlayer getMp () {
        return mp;