我正在尝试设计主屏幕上有3个按钮的应用程序;当用户按下前两个按钮时,它播放位于原始文件夹中的不同音乐。主屏幕上的第三个按钮应该将用户带到下一个屏幕,该屏幕还有2个按钮。
我试图在模拟器上运行我的应用程序。它在主屏幕的前两个按钮上播放音乐,当我点击第三个(下一个)按钮时,它将用户带到下一个屏幕。但是,当我点击第二个屏幕的第一个或第二个按钮时。它说"不幸的是你的应用程序已停止"。我现在不知道我的SecondActivity.Java代码有什么问题。
任何帮助将不胜感激。下面是我的主要和第二类JAVA代码。我确实在manifest.xml文件中声明了我的第二个Activity。
主要活动Java代码..
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;
}
}
}
答案 0 :(得分:1)
在您的第二项活动中,创建您的mediapPlayer AFTER 切换案例。在你的路上,他将尝试重现仍然不存在的东西:
@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();
}
}
<强>更新强> 在onclick()的任何操作之前执行mp.release():
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();
}
}
答案 1 :(得分:1)
我认为您的代码应该是这样的.. 删除
mp = MediaPlayer.create(this, resId);
mp.start();
切换语句前的
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;
}
答案 2 :(得分:0)
错误说明如下:
Resources not found with id of 0x01
意思是问题是:
int resId = 1;
mp = MediaPlayer.create(this, resId);
这是一个不存在的资源ID。
如果您的/ res / raw文件夹中包含这些文件,则应使用R.raw.xyz
ID。
虽然我猜这个问题确实只是
// 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;
}
}
混在一起而不是
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();