What's wrong with the Second Activity Java cod

2019-08-10 16:15发布

I am trying to design the app that has 3 buttons on the main screen; when the user presses on the first two buttons it plays the different music located in the raw folder. The third button on the main screen should drive the user to the next screen which also has 2 buttons.

I tried to run my app on the emulator. It plays the music on first two buttons of the main screen, and when I click on the third (Next) button it takes the user to the next screen. However, when I click on 1st or 2nd button of the Second Screen. It says "Unfortunately your app has stopped". I don't now what's wrong with my SecondActivity.Java code .

Any help will be greatly appreciated. Below is my MAIN and Second class JAVA code. I did declare my second Activity in manifest.xml file.

Main Activity Java Code..

    import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;




public class MainActivity extends Activity implements OnClickListener {
    private MediaPlayer mp;

   @Override
   public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      setVolumeControlStream(AudioManager.STREAM_MUSIC);
      findViewById(R.id.button_1).setOnClickListener(this);
      findViewById(R.id.button_2).setOnClickListener(this);
      findViewById(R.id.button_3).setOnClickListener(this);
   }
   public void onClick(View v) {   
       int resId=1;
      switch (v.getId()) {
      case R.id.button_1: resId = R.raw.button_1; break;
      case R.id.button_2: resId = R.raw.button_2; break;
      case R.id.button_3:
         startActivity(new Intent(MainActivity.this,SecondActivity.class));
         return;

      }
   // Release any resources from previous MediaPlayer
      if (mp != null) {   
          mp.release(); 
      }   
   // Create a new MediaPlayer to play this sound
      mp = MediaPlayer.create(this, resId); 
      mp.start();
   }
}

SecondActivity.Java

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;




public class SecondActivity extends Activity implements OnClickListener {
    private MediaPlayer mp;



   @Override
   public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_second);
      setVolumeControlStream(AudioManager.STREAM_MUSIC);
      findViewById(R.id.button_4).setOnClickListener(this);
      findViewById(R.id.button_5).setOnClickListener(this);
   }
   public void onClick(View v) {
        int resId = 1;
        // Release any resources from previous MediaPlayer
        if (mp != null) {               
            mp.release();   
        }

        // Create a new MediaPlayer to play this sound
        mp = MediaPlayer.create(this, resId); 
        mp.start();

        switch (v.getId()) {
          case R.id.button_4: resId = R.raw.button_4; break;
          case R.id.button_5: resId = R.raw.button_5; break;
        }
       }
    }

3条回答
一夜七次
2楼-- · 2019-08-10 16:34

The error says the following:

Resources not found with id of 0x01 

meaning the problem is this:

int resId = 1;
mp = MediaPlayer.create(this, resId); 

That is a resource ID that does not exist.

If you have these files in your /res/raw folder, then you should use the R.raw.xyz ID.

Although I guess the problem really is just that the

   // Create a new MediaPlayer to play this sound
    mp = MediaPlayer.create(this, resId); 
    mp.start();

    switch (v.getId()) {
      case R.id.button_4: resId = R.raw.button_4; break;
      case R.id.button_5: resId = R.raw.button_5; break;
    }
   }

Are mixed up instead of

    switch (v.getId()) {
      case R.id.button_4: resId = R.raw.button_4; break;
      case R.id.button_5: resId = R.raw.button_5; break;
    }
   }

   // Create a new MediaPlayer to play this sound
    mp = MediaPlayer.create(this, resId); 
    mp.start();
查看更多
等我变得足够好
3楼-- · 2019-08-10 16:44

I think your code should like this.. remove

 mp = MediaPlayer.create(this, resId); 
        mp.start();

before switch statement

    switch (v.getId()) {
      case R.id.button_4:
          resId = R.raw.button_4; 
          mp = MediaPlayer.create(this, resId); 
          mp.start();
          break;
      case R.id.button_5:
      resId = R.raw.button_5;
      mp = MediaPlayer.create(this, resId); 
      mp.start(); 
      break;
    }
查看更多
劳资没心,怎么记你
4楼-- · 2019-08-10 16:44

on your second activity, create your mediapPlayer AFTER switch case. On your way, he will try to reproduce something that still doesn't exist:

 @Override
   public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_second);
      setVolumeControlStream(AudioManager.STREAM_MUSIC);
      findViewById(R.id.button_4).setOnClickListener(this);
      findViewById(R.id.button_5).setOnClickListener(this);
   }
   public void onClick(View v) {
        int resId = 1;


        switch (v.getId()) {
          case R.id.button_4: resId = R.raw.button_4; break;
          case R.id.button_5: resId = R.raw.button_5; break;
        }

        // Release any resources from previous MediaPlayer
        if (mp != null) {               
            mp.release();   
        }

        // Create a new MediaPlayer to play this sound
        mp = MediaPlayer.create(this, resId); 
        mp.start();
       }
    }

UPDATE: Execute the mp.release() before any action on your onclick():

public class MainActivity extends Activity implements OnClickListener {
    private MediaPlayer mp;

   @Override
   public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      setVolumeControlStream(AudioManager.STREAM_MUSIC);
      findViewById(R.id.button_1).setOnClickListener(this);
      findViewById(R.id.button_2).setOnClickListener(this);
      findViewById(R.id.button_3).setOnClickListener(this);
   }
   public void onClick(View v) {   
       int resId=1;

   // Release any resources from previous MediaPlayer
      if (mp != null) {   
          mp.release(); 
      } 

      switch (v.getId()) {
      case R.id.button_1: resId = R.raw.button_1; break;
      case R.id.button_2: resId = R.raw.button_2; break;
      case R.id.button_3:
         startActivity(new Intent(MainActivity.this,SecondActivity.class));
         break;

      }

   // Create a new MediaPlayer to play this sound
      mp = MediaPlayer.create(this, resId); 
      mp.start();
   }
}
查看更多
登录 后发表回答