我已经从我在课堂上编写的另一个应用程序进行了研究,该应用程序使用Intent
更改为另一个类和布局,我认为代码与下面的代码类似。事实上,当我点击“新游戏”时,按钮它只是崩溃。我一直在评论.stop
的{{1}}和.release
函数,我确实注意到它会在尝试进入MediaPlayer
之前杀死音乐(然后崩溃) 。我感谢任何帮助,我花了几天时间试图挖掘论坛:)
MainActivity.java:
Intent
}
Game.java:
public class MainActivity extends Activity {
private MediaPlayer mpTheme;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mpTheme=MediaPlayer.create(MainActivity.this, R.raw.sttngtheme);
mpTheme.start();
Button newGameButton = (Button) findViewById(R.id.newGameButton);
Button loadGameButton = (Button) findViewById(R.id.loadGameButton);
Button instructionsButton = (Button) findViewById(R.id.instructionsButton);
final MainActivity mainActivity = this;
newGameButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mpTheme.stop();
mpTheme.release();
//Toast.makeText(getBaseContext(), "You clicked New Game", Toast.LENGTH_LONG).show();
Intent startGameIntent = new Intent(mainActivity, Game.class);
startActivity(startGameIntent);
}
});
loadGameButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Loading capability not yet coded.", Toast.LENGTH_LONG).show();
}
});
instructionsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Instructions not yet coded.", Toast.LENGTH_LONG).show();
}
});
}//end onCreate
@Override
protected void onPause(){
super.onPause();
if(mpTheme.isPlaying()){
mpTheme.pause();
}
}
@Override
protected void onStop(){
super.onStop();
if(mpTheme.isPlaying()){
mpTheme.stop();
mpTheme.release();
}
}
@Override
protected void onResume(){
super.onResume();
if(mpTheme.isPlaying()==false){
mpTheme.start();
}
}
}
logcat的:
public class Game extends Activity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.quadrantspace);
}
答案 0 :(得分:1)
在清单文件中添加目标活动。有关如何将Game.class添加到Manifest的更多信息,请查看official docs
此外,您无需单独声明mainActivity
变量即可在newGameButton
setOnClickListener
方法中使用。你只需要创建一个这样的意图 -
Intent i = new Intent(MainActivity.this, Game.class);
startActivity(i);