Android layout only showing after oncreate method

2019-08-17 18:14发布

I have created (what I thought was) a simple JAVA card game. In JAVA, the game loads the layout (frame) and displays ImageButtons consisting of a square like cardholder image for each player. The cardholders are then populated by random cards from the deck one by one.

So, in my onCreate method in Android, my initial layout consists of the cards with the card holder image, and then I run a method in the onCreate method to deal the cards. The problem I have is the initial layout doesn't show up at all. What shows up is the final layout with the cardholders already populated. The program doesn't even show them as they are individually being dealt I considered that maybe I need to run AsyncTask but I really don't have any "major" instructions in my deal method which would seem to require it.

I'm a relatively newbie to Android development and I guess I'm wondering, if something so simple is going to hang my original layout, where do main application instructions go if not in methods being called from the onCreate method? Why isn't it as simple as merely displaying my original layout, and then continuing the UI thread with the updating of the cards one-by-one?

UPDATE:

Unfortunately, it still doesn't work. My onCreate looks like this:

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            player1Card1B = (ImageButton) findViewById(R.id.p1C1Bref);
            player1Card2B = (ImageButton) findViewById(R.id.p1C2Bref);
            player1Card3B = (ImageButton) findViewById(R.id.p1C3Bref);
            player2Card1B = (ImageButton) findViewById(R.id.p2C1Bref);
            player2Card2B = (ImageButton) findViewById(R.id.p2C2Bref);
            player2Card3B = (ImageButton) findViewById(R.id.p2C3Bref);
            player3Card1B = (ImageButton) findViewById(R.id.p3C1Bref);
            player3Card2B = (ImageButton) findViewById(R.id.p3C2Bref);
            player3Card3B = (ImageButton) findViewById(R.id.p3C3Bref);
            player4Card1B = (ImageButton) findViewById(R.id.p4C1Bref);
            player4Card2B = (ImageButton) findViewById(R.id.p4C2Bref);
            player4Card3B = (ImageButton) findViewById(R.id.p4C3Bref);

            newDeckCard = (ImageButton) findViewById(R.id.newCardDeckBref);
            discardDeckCard = (ImageButton) findViewById(R.id.discardCardDeckBref);
            knockButton = (ImageButton) findViewById(R.id.knockBref);

            player1Card1B.setOnClickListener(this);
            player1Card2B.setOnClickListener(this);        
            player1Card3B.setOnClickListener(this);
            newDeckCard.setOnClickListener(this);
            discardDeckCard.setOnClickListener(this);
            knockButton.setOnClickListener(this);
    }

and my onStart like this:

     @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();


        shuffle();
        deal();
    }

I just get a white screen that reads "Main" and then eventually the cards are shown already populated. I don't even get the placeholders that are read in the onCreate method. When I remove the deal() method, the placeholder cards show up. When I put it back, the placeholder cards don't show up and just the dealt cards show up.

1条回答
我想做一个坏孩纸
2楼-- · 2019-08-17 18:48

You should move the code that populates the cards into your activity's onResume() method - probably some sort of AsyncTask kicked off by onResume() that does the work over time, so that the user can visually see the changes.

The onResume() method is invoked when Android actually displays your activity on-screen; if you do do everything in onCreate() all work will be completed before your app ever appears on-screen - which is producing the results you're seeing.

Google has some good online documentation on how the activity lifecycle works.

查看更多
登录 后发表回答