查看 activity 中的视图

View view in activity

我正在尝试使我的 activity 正常工作,但方法 nextSong() 出现问题。它的最后一行,playorPauseMusic()有一个错误:-

playoyPauseMusic (View) in PlaySongActivity cannot be applied to ()

我不太确定问题出在哪里,但认为是关于 (View view)。我是 Java 的新手,所以如果您能详细说明问题,那将大有帮助。谢谢;)

public void playorPauseMusic(View view) 
{
    if (player == null) {
        preparePlayer();
    }
    if (!player.isPlaying()) {
        if (musicPosition > 0) {
            player.seekTo(musicPosition);
        }
        player.start();
        getSeekBarStatus();
        btnPlayPause.setText("PAUSE");
        setTitle("Now Playing: " + title + " = " + artist);
        gracefullyStopWhenMusicEnds();
    } else {
        pauseMusic();
    }
}

private void gracefullyStopWhenMusicEnds()
{
    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            nextSong();
        }
    });
}

public void stopActivities() {
    btnPlayPause.setText("PLAY");
    musicPosition = 0;
    setTitle("");
    player.stop();
    player.release();
    player = null;
}


private void nextSong()
{
    Song nextSong = songCollection.getNextSong(songId);
    if (nextSong !=null)
    {
        songId = nextSong.getId();
        title = nextSong.getTitle();
        artist = nextSong.getartist();
        fileLink = nextSong.getFileLink();
        coverArt = nextSong.getCoverArt();
        url = BASE_URL + fileLink;
        displaySong(title, artist, coverArt);
        stopActivities();
        playorPauseMusic();
    }
}

您的方法 playorPauseMusic(View view) 有一个参数 View view。这意味着无论何时调用该方法,都必须提供一个 View 实例作为参数。在 nextSong() 方法的最后一行中,您在没有视图参数的情况下调用 playorPauseMusic(),导致错误

尽管您似乎并未实际使用该参数,因此您可以将方法 playorPauseMusic(View view) 更改为仅 playorPauseMusic()