我在第一个Activity完成后正在开发一个应用程序。我通过mediaplayer
开始音乐,并在上一次活动中浏览了许多活动。我无法停止mediaplayer。我已经调用了第一种活动类方法来停止mediaplayer,但它显示出nullpointer异常,任何人都可以为我提供相关的解决方案,我没有使用过服务类。Android中的MediaPlayer问题
下面显示的是我开始mediaplayer的第一个活动。
public class GamemenuActivity extends Activity implements OnClickListener {
Button newgame, highscore, help,sound;
MediaPlayer mp;
int i=0;
TranslateAnimation anim, anim511, anim2511,animsound;
BounceInterpolator bo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menuscreen);
media();
newgame = (Button) findViewById(R.id.newgamebutton);
highscore = (Button) findViewById(R.id.highscoresButton);
help = (Button) findViewById(R.id.helpbutton);
sound=(Button)findViewById(R.id.soundbutton);
anim = new TranslateAnimation(3250, 0, 100, 0);
anim511 = new TranslateAnimation(3250, 0, 100, 0);
anim2511 = new TranslateAnimation(3250, 0, 100, 0);
animsound=new TranslateAnimation(3250, 0,100,0);
anim.setFillAfter(true);
newgame.startAnimation(anim);
anim511.setDuration(3000);
anim.setDuration(3000);
anim511.setFillAfter(true);
highscore.startAnimation(anim511);
anim2511.setDuration(3000);
anim2511.setFillAfter(true);
help.startAnimation(anim2511);
animsound.setDuration(3000);
animsound.setFillAfter(true);
sound.startAnimation(animsound);
newgame.setOnClickListener(this);
highscore.setOnClickListener(this);
help.setOnClickListener(this);
sound.setOnClickListener(this);
}
public void media() {
/* Intent i=new Intent(GamemenuActivity.this,Background.class);
startService(i);*/
mp = MediaPlayer.create(getApplicationContext(), R.raw.gameback);
mp.start();
mp.setVolume(1, 1);
mp.setLooping(true);
}
public void stopmedia(){
mp.stop();
mp.release();
}
,我想结束音乐活动性低于
public class Highpage extends Activity{
TextView name,score;
int sli;
Button sub;
Bundle base;
SQLiteHelper sqdb;
String data,val;
GamemenuActivity gm;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
gm=new GamemenuActivity();
TextView name=new TextView(this);
TextView score=new TextView(this);
base=getIntent().getExtras();
data=base.getString("a");
val=base.getString("b");
sli=base.getInt("brk");
name.setText(data);
score.setText(val);
System.out.println("values are"+data +val);
sqdb=new SQLiteHelper(this);
sqdb.insertvalues(data, val,sli);
Toast.makeText(Highpage.this, "values updated successfully", 30).show();
//stopping media player
gm.stopmedia();
Intent hm=new Intent(Highpage.this,GamemenuActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(hm);
finish();
}
}
这是logcat的
12-23 14:54:37.179: E/AndroidRuntime(11970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-23 14:54:37.179: E/AndroidRuntime(11970): at dalvik.system.NativeStart.main(Native Method)
12-23 14:54:37.179: E/AndroidRuntime(11970): Caused by: java.lang.NullPointerException
12-23 14:54:37.179: E/AndroidRuntime(11970): at mobbi.Stallion.imageteaser.GamemenuActivity.stopmedia(GamemenuActivity.java:80)
播放后释放您的mediaplayer。 – Rahul
你说要做mp.stop(); mp.release();我已经完成了最后一个活动,我必须停止mediaplayer但不工作 –
然后把你的代码。 – Rahul