布局并不总是加载

时间:2016-12-29 07:44:50

标签: android

我通过简化代码编程重新设计了我的项目。我通过xml布局放置了所有静态图像。

我运行程序时只得到2个结果。

1)运行没有问题

enter image description here

2)运行问题

enter image description here

我收到以下错误:

Connected to process 10651 on device 4.7_WXGA_API_22 [emulator-5554]
I/art: Not late-enabling -Xcheck:jni (already on)
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable

我认为问题出在这个内部课堂的某个地方,我想?!

 private class MatchCardGame{

    private Game mMatchGame;

    private List<Drawable> revealImagesOfCards;
    private List<Integer> revealCards;
    private Drawable hiddenCard;
    private List<Integer> cardPoints;
    private List< Boolean> isHidden;

    public MatchCardGame(int numOfCards){

        mMatchGame = new Game(numOfCards);
        revealImagesOfCards = new ArrayList<>();
        revealCards = new ArrayList<>();
        cardPoints = new ArrayList<>();
        isHidden = new ArrayList<>();

        setCoverCard();

        for(int i = 1; i <= numOfCards; i++)
            setMatchImageCard(i);

    }
    public void setMatchImageCard( int cardLoc){

        int drawableLoc =  mMatchGame.findImageOfCard(cardLoc);
       Drawable drawable = ResourcesCompat.getDrawable(getResources(), drawableLoc, null);
        Integer revealCard = mMatchGame.findContentsOfCard(cardLoc);

        revealImagesOfCards.add(drawable);
        revealCards.add(revealCard);
        cardPoints.add(Integer.valueOf(20));
        Boolean hideCard = true;
        isHidden.add(hideCard);

    }
    private void setCoverCard(){
        hiddenCard = ResourcesCompat.getDrawable(getResources(), R.drawable.black_card, null);
    }

    public Drawable getImage(int loc, boolean statReveal){
        loc--;
        if(!statReveal){
            Boolean hideCard = isHidden.get(loc);
            hideCard = true;
            return hiddenCard;
        }
        else {
            Boolean hideCard = isHidden.get(loc);
            hideCard = false;
            return revealImagesOfCards.get(loc);
        }



    }

    public boolean getHiddenStat(int loc){
        loc--;
        Boolean hideCard = isHidden.get(loc);
        return  hideCard;
    }

    public boolean compareCards(int loc1, int loc2){
        loc1--;
        loc2--;

        Integer card1 = revealCards.get(loc1);
        Integer card2 = revealCards.get(loc2);
        Integer cardPts1 = cardPoints.get(loc1);
        Integer cardPts2 = cardPoints.get(loc2);


        if(card1 == card2){

            int num = Integer.valueOf( scoreText.getText().toString());

            Log.i("TAGG","Score Points: " + (cardPts1 + cardPts2));
            new AdjustScore().execute(Integer.valueOf(cardPts1 + cardPts2));


            return true;
        }

        else{


            cardPts1 -= 5;
            cardPts2 -= 5;


            if(cardPts1 < 0)
                cardPts1 = 0;

            if(cardPts2 < 0)
                cardPts2 = 0;

            cardPoints.set(loc1, cardPts1);
            cardPoints.set(loc2,cardPts2);

            return false;
        }


    }

    private class AdjustScore extends AsyncTask<Integer,Integer,Void>{

        private TextView scoreText;
        private int currentScore;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            scoreText = (TextView) findViewById(R.id.score_txt);
            currentScore = Integer.valueOf( scoreText.getText().toString());
        }

        @Override
        protected Void doInBackground(Integer... integers) {
          final  int num = integers[0];

            Runnable runnable = new Runnable() {
                @Override
                public void run() {

            for (int x = 1; x <= num; x++){


                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                      publishProgress(Integer.valueOf(currentScore + x));
                    }


            }
        };
        new Thread(runnable).start();

        return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            Message msg = scoreHandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("myPoints",String.valueOf(values[0]));



            msg.setData(bundle);
            scoreHandler.sendMessage(msg);

        }
    }


}

我在onCreate of activity中调用了这个

private MatchCardGame myGame;
private List<Integer> selectCards;
private TextView scoreText;

private Handler scoreHandler = new Handler(){

    @Override
    public void handleMessage(Message msg) {

        Bundle bundle = msg.getData();
        String stat1 = bundle.getString("myPoints");

        int num = Integer.valueOf(stat1);


        scoreText.setText(String.valueOf(num));


    }


};
@Override
protected void onCreate( Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_card_game);

    myGame = new MatchCardGame(12);
}

我有另一个派生自AsynTask的内部类。 它主要用于设置卡片视图并在imageview上设置clicklisteners。但我不认为这是问题所在。

1 个答案:

答案 0 :(得分:2)

首先,这些都不是错误。他们是垃圾收集者。这完全正常,除非您同时遇到性能问题,否则不应该给您任何担忧。在代码中而不是在xml中执行某些操作以避免这些操作是不必要的,并且在需要垃圾收集器的情况下基于纯粹的运气来工作或不工作。

其次,您要在AsyncTask中加载所有图像。有时这很好(它阻止你暂停主线程加载图片),但是如果你没有适当的默认图像,那么在完成任务之前它实际上无法显示任何图片。因此,在绘图和任务完成之间存在竞争条件。

解决方案:在xml中执行,或者设置加载屏幕。我建议第一个,因为如果图像是来自应用程序的静态图像,那么你的任务实际上并没有做任何有用的事情 - 那些是在应用程序启动时加载的。