简体   繁体   中英

Code in Android onCreate() method not working properly

I'm trying to make a really simple android game, and so far I have a method named startGame() which just plays a sound and outputs a random number to logcat as a test.
I call startGame() in the onStart() method of my game Activity but when I do that the setContentView() in my onCreate() does not seem to work.
I get the sound playing but the GameActivity 's layout does not appear, and instead the sound plays over the main menu activity.

@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();
}

Here is my XML file, although it worked perfectly fine to just display the layout it didn't work once I tried to run the other piece of code:

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

My game method:

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));

}

Your XML layout dosn't have root element. That's the problem

I figured it out thanks to Vikas.
The problem was that I hadn't set up the onStart() properly. I needed to add the line:

super.onStart();  

for it to work. Thanks everyone who tried to help :)

why don't you call startGame() in onCreate() directly?

you can try with this:
Paste this in your 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);
}

EDIT
Now i tried same code and it's displaying the activity, so now you can replace your full activity class as below.

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));
    }
}

Call your startGame method after setContentView(R.layout.activity_game); in onCreate() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM