Android动态布局仅在oncreate方法完成后显示

时间:2016-05-16 00:08:44

标签: android sqlite android-layout

   isAnswered=0;
 @Override
        protected void onCreate(Bundle savedInstanceState) {
            Log.d(TAG,"1isanswered : "+isAnswered);
            Log.d(TAG, "matchinggame OnCreate");
            super.onCreate(savedInstanceState);
            dbHandler = new DatabaseHandler(this);
            rLayout = new RelativeLayout(this);
            object = new ObjectObject();
            imageAnswer=new ImageView(this);
            setContentView(rLayout);


            trainingID = getIntent().getExtras().getInt("trainingid");
            trainingObjectList = new ArrayList<TrainingObject>();

            trainingObjectList = dbHandler.getAllTrainingObject(trainingID);
    //// TODO: 02.05.2016 egitim kismi oalcak burda tek tek gosterilecek

            rLayout.setBackgroundColor(Color.MAGENTA);





           // Handler handler = new Handler(); Log.d(TAG,"2isanswered : "+isAnswered);
            for(int i=0;i<2;i++){Log.d(TAG,"i:"+i); Log.d(TAG,"3tisanswered : "+isAnswered);
                final int finalI = i;


                //  handler.postDelayed(new Runnable() {

                  //  @Override
               //     public void run() {Log.d(TAG,"thread i: "+finalI);
                        Log.d(TAG,"4isanswered : "+isAnswered);
                 isAnswered = 0;
                TrainingObject trainingObject = new TrainingObject();
                trainingObject = trainingObjectList.get(finalI);
                objectCount = 2;
    //test icin
                Log.d(TAG,"testicin trainingobjectid: "+trainingObject.getTrainingobjectID());
                object = dbHandler.getObjectObject(trainingObject.getTrainingobjectAnswer());

                if(trainingObject.getTrainingobjectThree()!=0) objectCount++;
                if(trainingObject.getTrainingobjectFour()!=0) objectCount++;
                if(trainingObject.getTrainingobjectFive()!=0) objectCount++;
                Log.d(TAG,"matchinggame objcount: "+objectCount);

                RelativeLayout.LayoutParams rLayParams = new RelativeLayout.LayoutParams(140,140);

                rLayParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);

                 imgBytes = object.getObjectImageBlob();
                 bmp = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);

                imageAnswer.setImageBitmap(bmp);
                imageAnswer.setTag(trainingObject.getTrainingobjectAnswer());
                imageAnswer.setId(R.id.imgAnswer);
                rLayout.removeAllViews();
                rLayout.addView(imageAnswer,rLayParams);




                        imageOne.setOnDragListener(MatchingGame.this);


                imageAnswer.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) { Log.d(TAG,"6isanswered : "+isAnswered);
                        Log.d(TAG,"matchinggame setontouch");
                        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                            Log.d(TAG,"matchinggame setontouch if yes");
                            ClipData data = ClipData.newPlainText("", "");
                            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                            v.startDrag(data, shadowBuilder, v, 0);
                            v.setVisibility(View.INVISIBLE); Log.d(TAG,"7isanswered : "+isAnswered);
                            return true;
                        } else { Log.d(TAG,"8isanswered : "+isAnswered);
                            return false;
                        }
                    }
                });

                imageAnswer.setOnDragListener(MatchingGame.this);
                Log.d(TAG,"*");
                while(isAnswered==0){
                    //Log.d(TAG,"*");
                }

                //  }
              //  }, 30000*finalI );Log.d(TAG,"thread sonrasi: "+finalI); Log.d(TAG,"5isanswered : "+isAnswered);
            }Log.d(TAG,"ff");
        }

因为isanswered = 0,它保持无限循环。但我看不到我的布局,我只看到洋红色,而这个循环继续。几秒钟后黑屏就来了。

但是当我在那里禁用时,在将ff打印到log后,它会完成oncreate然后布局更改。

我搜索但没有回答。

Android layout only showing after oncreate method finishes

这里说试试onstart。是的,我试过但是一样。

我这里只有asynctask调用来从sqlite获取值。

我有一个for循环,在每次迭代中,我将显示图像,用户将尝试匹配它们。

我没有使用视图或片段。

为什么一看到它就不会发布到屏幕上?

完整的活动在这里:

https://gist.github.com/anonymous/87ccd4147ae0d202244bb78f51844f29

1 个答案:

答案 0 :(得分:1)

这是因为在onCreate方法完成之前未显示活动。尝试将代码移至onResume,看看它是否有效。此外,在您当前的代码中,请不要忘记致电super.onCreate(savedInstanceState)