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
package com.app.milsan.learncro;

import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class StartActivity extends Activity {

   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(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(R.id.sound_button);
              soundPlay001.setOnClickListener(new View.OnClickListener() {
              public void onClick (View v){
              mpButtonClick001.start();
                   }}); break;
          case 2:  setContentView(R.layout.a002);
           final Button soundPlay002 = (Button) findViewById(R.id.sound_button);
           soundPlay002.setOnClickListener(new View.OnClickListener() {
              public void onClick (View v){
              mpButtonClick002.start();
           }}); break;
          case 3:  setContentView(R.layout.a003); 
           final Button soundPlay003 = (Button) findViewById(R.id.sound_button);
           soundPlay003.setOnClickListener(new View.OnClickListener() {
              public void onClick (View v){
              mpButtonClick003.start();
           }}); break;
          case 4:  setContentView(R.layout.a004); 
           final Button soundPlay004 = (Button) findViewById(R.id.sound_button);
           soundPlay004.setOnClickListener(new View.OnClickListener() {
              public void onClick (View v){
              mpButtonClick004.start();
           }}); break;
        default: setContentView(R.layout.black); break;          
              }
     final Button startButton = (Button) findViewById(R.id.start_button);
          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(R.layout.black); break;
             }
             Intent nextA = new Intent (StartActivity.this, GoBackActivity.class);
             startActivity(nextA);
             finish();   
                }});}}
ajoj11
Freshman
Freshman
 
Posts: 2
Joined: Tue Feb 22, 2011 6:48 pm

Top

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.

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

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 11 guests