App crash after reloading Activity with sound

All your problems with Audio, Video and Images.

App crash after reloading Activity with sound

Postby ajoj11 » Tue Feb 22, 2011 7:00 pm

Hi there

I have a problem with my app. I have made a button that reloads my main activity and every time it reloads it generates a random number. Depending on the number another sound will play (all sounds are in MP3 format). Now after reloading my main activity to many times (between 6-9) the program crashes and i don't know why. I hope you can help me with this:

Code: Select all

import java.util.Random;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class StartActivity extends Activity {

   protected void onCreate(Bundle savedInstanceState) {
      final MediaPlayer mpButtonClick001 = MediaPlayer.create(this, R.raw.a001);
      final MediaPlayer mpButtonClick002 = MediaPlayer.create(this, R.raw.a002);
      final MediaPlayer mpButtonClick003 = MediaPlayer.create(this, R.raw.a003);
      final MediaPlayer mpButtonClick004 = MediaPlayer.create(this, R.raw.a004);
      final int number = (int)(Math.random() * 3) + 1;
              switch (number) {
          case 1:     setContentView(R.layout.a001);                 
             final Button soundPlay001 = (Button) findViewById(;
              soundPlay001.setOnClickListener(new View.OnClickListener() {
              public void onClick (View v){
                   }}); break;
          case 2:  setContentView(R.layout.a002);
           final Button soundPlay002 = (Button) findViewById(;
           soundPlay002.setOnClickListener(new View.OnClickListener() {
              public void onClick (View v){
           }}); break;
          case 3:  setContentView(R.layout.a003); 
           final Button soundPlay003 = (Button) findViewById(;
           soundPlay003.setOnClickListener(new View.OnClickListener() {
              public void onClick (View v){
           }}); break;
          case 4:  setContentView(R.layout.a004); 
           final Button soundPlay004 = (Button) findViewById(;
           soundPlay004.setOnClickListener(new View.OnClickListener() {
              public void onClick (View v){
           }}); break;
        default: setContentView(; break;          
     final Button startButton = (Button) findViewById(;
          startButton.setOnClickListener(new View.OnClickListener(){
             public void onClick(View V) {
             switch (number) {
             case 1: mpButtonClick001.stop(); break;
             case 2: mpButtonClick002.stop(); break;
             case 3: mpButtonClick003.stop(); break;
             case 4: mpButtonClick004.stop(); break;
             default: setContentView(; break;
             Intent nextA = new Intent (StartActivity.this, GoBackActivity.class);
Posts: 2
Joined: Tue Feb 22, 2011 6:48 pm


Re: App crash after reloading Activity with sound

Postby mitutitu » Sun Feb 27, 2011 12:26 am

Hello ajoj11,
I think it is happening because of too many setContentView and their random replacement. I don't know which version of SDK you are using but I found this problem in 1.5 and 1.6 OS.

Moreover, you can see your logCat crash report. It will give clear view of the problem.

Android is awesome!
User avatar
Senior Developer
Senior Developer
Posts: 113
Joined: Tue Jun 09, 2009 7:40 am
Location: St. John's, NL, Canada


Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 10 guests