Android布局仅在oncreate方法完成后显示 [英] Android layout only showing after oncreate method finishes

查看:228
本文介绍了Android布局仅在oncreate方法完成后显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了(我当时以为是)一个简单的JAVA纸牌游戏.在JAVA中,游戏会加载布局(框架)并为每个玩家显示由正方形的持卡人图像组成的ImageButton.然后,从卡组中一张一张地随机抽出卡.

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.

因此,在Android的onCreate方法中,我的初始布局由带有持卡人图片的卡片组成,然后在onCreate方法中运行一个方法来处理卡片.我的问题是初始布局根本没有显示.显示的是已填充持卡人的最终布局.该程序甚至没有显示它们,因为它们是单独处理的,我认为也许我需要运行AsyncTask,但是我的交易方法中确实没有任何主要"指令,而这似乎是必需的.

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.

我是Android开发的一个相对较新手,我想我想知道,是否有这么简单的事情挂在我的原始布局上,如果不在onCreate方法中调用的方法中,主应用程序指令会转到哪里?为什么不像只显示我的原始布局,然后继续UI线程并逐张更新卡片一样简单?

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?

更新:

不幸的是,它仍然无法正常工作.我的onCreate看起来像这样:

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

我只得到一个白色的屏幕,上面写着"Main"(主),然后最终显示出已填充的卡.我什至没有获得onCreate方法中读取的占位符.当我删除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.

推荐答案

您应该将填充卡片的代码移到活动的onResume()方法中-可能是onResume()开始的某种AsyncTask随着时间的推移而工作,以便用户可以直观地看到更改.

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.

当Android实际上在屏幕上显示您的活动时,将调用onResume()方法;如果您执行onCreate()中的所有操作,则所有工作都将在您的应用程序出现在屏幕上之前完成-这将产生您所看到的结果.

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有一些好的在线文档生命周期有效.

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

这篇关于Android布局仅在oncreate方法完成后显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆