Java Android onCreate()方法中的代码工作不正常

Java Android onCreate()方法中的代码工作不正常,java,android,android-studio,Java,Android,Android Studio,我正在尝试制作一个非常简单的android游戏,到目前为止,我有一个名为startGame()的方法,它只播放一个声音并向logcat输出一个随机数作为测试。 我在游戏活动的onStart()方法中调用startGame(),但当我这样做时,我的onCreate()中的setContentView()似乎不起作用。 我可以播放声音,但是游戏活动的布局没有显示,而是通过主菜单活动播放声音 @Override protected void onCreate(Bundle savedInstanceS

我正在尝试制作一个非常简单的android游戏,到目前为止,我有一个名为
startGame()
的方法,它只播放一个声音并向logcat输出一个随机数作为测试。
我在游戏
活动的
onStart()
方法中调用
startGame()
,但当我这样做时,我的
onCreate()中的
setContentView()
似乎不起作用。
我可以播放声音,但是
游戏活动
的布局没有显示,而是通过主菜单活动播放声音

@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"/>

}为什么不直接在onCreate()中调用startName()

您可以试试这个:
将其粘贴到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));
    }
}

为什么不直接在onCreate()中调用startName()

您可以试试这个:
将其粘贴到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));
    }
}

您的XML布局没有根元素。这就是问题所在

您的XML布局没有根元素。这就是问题所在

setContentView(R.layout.activity\u游戏)之后调用startName方法onCreate()
方法中进行编码。

setContentView(R.layout.activity\u游戏)之后调用startName方法
onCreate()
方法中。

多亏了Vikas,我才找到了它。
问题是我没有正确设置onStart()。我需要添加一行:

super.onStart();  
让它工作。
感谢所有试图帮助我的人:)

多亏了维卡斯,我才明白过来。
问题是我没有正确设置onStart()。我需要添加一行:

super.onStart();  
让它工作。
感谢所有试图提供帮助的人:)

能否显示activity_game.xml在OnResume()中调用startGame()方法。调用OnResume()也有同样的问题。我将在上面的编辑中添加xml文件。您的xml没有根元素?在onStart()方法中,您可以添加super.onStart();能否在OnResume()中显示activity_game.xml调用startGame()方法。调用OnResume()也有同样的问题。我将在上面的编辑中添加xml文件。您的xml没有根元素?在onStart()方法中,您可以添加super.onStart();这应该是一条注释。很简单,您可以在setContentView()之后在onCreae()中调用startName(),是的-如果没有方法调用,它可以正常工作。因此,请发布您的方法代码。问题就在这里。好的,我马上发布:)这应该是一个注释。很简单,你可以在setContentView()之后在onCreae()中调用startName(),是的-如果没有方法调用,它工作得非常好。因此,请发布你的方法代码。问题就在这里。好的,我马上就发布:)首先,当你发布你在上根RelativeLayout没有的时候。在这之后,你添加了,但是你忘了关闭布局末尾的布局啊,对,是的,对不起:)我没有正确格式化它,所以堆栈溢出没有显示它,哈哈,当你发布你没有在顶部根相对论的时候。在这之后你添加了,但是你忘了关闭布局末尾的布局啊,是的,对不起:)我没有正确格式化它,所以堆栈溢出没有显示它哈哈