第一个活动完成后,我正在开发一个应用程序。我正在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)