获取java.lang.IllegalStateException

时间:2014-11-01 07:21:07

标签: java android audio media-player android-mediaplayer

Activity A我正在播放一首歌,现在又转到Activity B这里再一个按钮播放另一首歌

现在,当我在活动B时,我必须检查Activity A's歌曲already playing是否必须向用户显示已在活动A上播放歌曲的消息,如果用户未在活动A上播放歌曲则用户可以开始在Activity B

中播放歌曲

ActivityB.java: -

btnPlay.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    if(ActivityA.mPlayer!=null && ActivityA.mPlayer.isPlaying()) // getting exception
    {
    Toast.makeText(Activity.this, "Already playing song on Activity A", Toast.LENGTH_LONG).show();
    }
    else 
    {
    mediaPlayer.start();
    btnPlay.setVisibility(View.GONE);
    btnPause.setVisibility(View.VISIBLE);   
      }
    }
});

现在当我getting这个Exception时:就像我在活动playing song中开始A,现在转到活动B,这里我{ {1}}按钮 - 它显示我的消息tapped on play然后我决定回到活动A,我点击停顿现在我没有在活动A中播放歌曲,我再次移动到活动B(这时我点击"Already playing song on Activity A"按钮获取Play)在此行:

IllegalStateException

if(ActivityA.mPlayer!=null && ActivityA.mPlayer.isPlaying())

1 个答案:

答案 0 :(得分:1)

我认为最好在ActivityA

中这样做
Intent i = new Intent(this, ActivityB.class);
i.putExtra("isPlaying", mPlayer.isPlaying);
startActivity(i);

ActivityB's onCreate

Bundle data = getIntent().getExtras();

if(data != null)
        {
            isPlayingBefore = data.getParcelable("isPlaying");
        }

onClick上:

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    if(isPlayingBefore)
    {
    Toast.makeText(Activity.this, "Already playing song on Activity A", Toast.LENGTH_LONG).show();
    }
    else 
    {
    mediaPlayer.start();
    btnPlay.setVisibility(View.GONE);
    btnPause.setVisibility(View.VISIBLE);   
      }
    }