Vibhs
Vibhs

Reputation: 1

onbackpressed() crashes

i have this code in which i want to go back to my previous activity, I'm all confused as I'm new to android. please help me this is my college project. this app plays sound on button touch but now i want to go back to my previous activity that is main menu.

ImageButton sol=(ImageButton)findViewById(R.id.e1);
sol.setOnClickListener(this);

ImageButton sol2=(ImageButton)findViewById(R.id.e2);
sol2.setOnClickListener(this);
}

/*protected void onResume()
{super.onResume();}*/

@Override
protected void onPause(){

    super.onPause();
    player.release();
    player=null;
    }


public void onClick(View v){

switch(v.getId()){
case R.id.e1:
    resId=R.raw.ambu;
    break;

case R.id.e2:
    resId=R.raw.pol;
    break;
}

if(player!=null)
    {player.release();}
player=MediaPlayer.create(this, resId);
player.start();}

public void onBackPressed(){
    Intent start = new Intent(emer.this,SprojectActivity.class);
     startActivity(start);
        finishActivity(0);}

Upvotes: 0

Views: 441

Answers (1)

nhaarman
nhaarman

Reputation: 100358

I think it crashes because you don't check if player isn't null in your onPause() method. Try to check for null first, or post your logcat.

Upvotes: 1

Related Questions