我试图制作一个非常简单的安卓游戏,到目前为止,我有一个名为startGame()
的方法,它只播放声音并输出一个随机数作为测试。
我在游戏的startGame()
方法onStart()
中调用了Activity
,但是当我这样做时,setContentView()
中的onCreate()
似乎不起作用。
我播放声音但GameActivity
的布局没有出现,而是声音播放主菜单活动。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_game);
//make the hardware volume control buttons affect the music stream instead of the ringer
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
public void onStart(){
startGame();
}
这是我的XML文件,虽然它只是显示一下我尝试运行另一段代码后它没有工作的布局非常好:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.wordpress.jakezachariahnixon.alphabetfarm.GameActivity">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@drawable/heart" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/imageView2"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/imageView3"
android:layout_toStartOf="@+id/imageView3"
android:background="@drawable/heart" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/imageView3"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/imageView"
android:layout_toStartOf="@+id/imageView"
android:background="@drawable/heart" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageView4"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/cow_no_background" />
<ImageButton
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/imageButton3"
android:layout_marginTop="60dp"
android:layout_marginLeft="35dp"
android:background="@drawable/btn_upper_a" />
<ImageButton
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/imageButton4"
android:background="@drawable/btn_upper_b"
android:layout_marginTop="60dp"
android:layout_marginLeft="180dp"/>
<ImageButton
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/imageButton5"
android:background="@drawable/btn_upper_c"
android:layout_marginTop="155dp"
android:layout_marginLeft="35dp" />
<ImageButton
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/imageButton6"
android:background="@drawable/btn_upper_d"
android:layout_marginTop="155dp"
android:layout_marginLeft="180dp"/>
<ImageButton
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/imageButton7"
android:background="@drawable/btn_upper_e"
android:layout_marginTop="250dp"
android:layout_marginLeft="35dp"/>
<ImageButton
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/imageButton8"
android:background="@drawable/btn_upper_f"
android:layout_marginTop="250dp"
android:layout_marginLeft="180dp"/>
我的游戏方法:
public void startGame(){
ImageButton button_a=(ImageButton)findViewById(R.id.imageButton3);
ImageButton button_f=(ImageButton)findViewById(R.id.imageButton8);
ImageButton button_e=(ImageButton)findViewById(R.id.imageButton7);
ImageButton button_d=(ImageButton)findViewById(R.id.imageButton6);
ImageButton button_c=(ImageButton)findViewById(R.id.imageButton5);
ImageButton button_b=(ImageButton)findViewById(R.id.imageButton4);
Random randomGenerator = new Random();
int letter = randomGenerator.nextInt(6);
int letterTapped = -1;
switch(letter){
case 0:
player= MediaPlayer.create(GameActivity.this, R.raw.a);
player.start();
break;
case 1:
player= MediaPlayer.create(GameActivity.this, R.raw.b);
player.start();
break;
case 2:
player= MediaPlayer.create(GameActivity.this, R.raw.c);
player.start();
break;
case 3:
player= MediaPlayer.create(GameActivity.this, R.raw.d);
player.start();
break;
case 4:
player= MediaPlayer.create(GameActivity.this, R.raw.e);
player.start();
break;
case 5:
player= MediaPlayer.create(GameActivity.this, R.raw.f);
player.start();
break;
}
Log.v("Sound played:", String.valueOf(letter));
}
答案 0 :(得分:3)
你的XML布局没有root元素。这就是问题
答案 1 :(得分:1)
感谢Vikas,我明白了 问题是我没有正确设置onStart()。我需要添加一行:
super.onStart();
让它发挥作用。 谢谢所有试图提供帮助的人:)
答案 2 :(得分:0)
为什么不直接在onCreate()中调用startGame()?
你可以试试这个:
将其粘贴到onCreate():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_game);
}
修改强>
现在我尝试了相同的代码并显示了活动,所以现在您可以替换下面的完整活动类。
public class GameActivity extends Activity {
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_game);
//make the hardware volume control buttons affect the music stream instead of the ringer
setVolumeControlStream(AudioManager.STREAM_MUSIC);
//startGame();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void startGame() {
ImageButton button_a = (ImageButton) findViewById(R.id.imageButton3);
ImageButton button_f = (ImageButton) findViewById(R.id.imageButton8);
ImageButton button_e = (ImageButton) findViewById(R.id.imageButton7);
ImageButton button_d = (ImageButton) findViewById(R.id.imageButton6);
ImageButton button_c = (ImageButton) findViewById(R.id.imageButton5);
ImageButton button_b = (ImageButton) findViewById(R.id.imageButton4);
Random randomGenerator = new Random();
int letter = randomGenerator.nextInt(6);
int letterTapped = -1;
switch (letter) {
case 0:
player = MediaPlayer.create(GameActivity.this, R.raw.a);
player.start();
break;
case 1:
player = MediaPlayer.create(GameActivity.this, R.raw.b);
player.start();
break;
case 2:
player = MediaPlayer.create(GameActivity.this, R.raw.c);
player.start();
break;
case 3:
player = MediaPlayer.create(GameActivity.this, R.raw.d);
player.start();
break;
case 4:
player = MediaPlayer.create(GameActivity.this, R.raw.e);
player.start();
break;
case 5:
player = MediaPlayer.create(GameActivity.this, R.raw.f);
player.start();
break;
}
Log.v("Sound played:", String.valueOf(letter));
}
}
答案 3 :(得分:0)
在setContentView(R.layout.activity_game);
方法中onCreate()
之后调用您的startGame方法。