1

i know what it means to do too much work on the main thread. But i am unable to determine where this occurs in code.

i get the results before the screen goes black

W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f2b3b03d840, error=EGL_SUCCESS
I/Choreographer: Skipped 89 frames!  The application may be doing too much work on its main thread.
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f2b3b03d400, error=EGL_SUCCESS
D/OpenGLRenderer: endAllStagingAnimators on 0x7f2b3b1e9800 (RippleDrawable) with handle 0x7f2b43ec6200
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f2b3b03d100, error=EGL_SUCCESS

i use ImageViews in my layout but must set images through code because when i set them through xml layout alot more work is done, and way more frames are skipped along with other errors.

my code:

public class GameActivity extends AppCompatActivity {

       private List<MatchImageCard> gameCards;
       private List<Drawable> gameOverCards;
       private List<ImageView> staticImages;
       private Game mMatchGame;

       @Override
       protected void onCreate( Bundle savedInstanceState) {

                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.activity_card_game);

                 mMatchGame = new Game(12);   //create game using class


                 assignStaticImages();       //get access to view normally set through xml layout
                 setStaticImages();      //set these views accessed
                 assignCards();         //access location of card view store in gameCards List  

       }

       private void assignStaticImages(){

              ImageView redStone = (ImageView) findViewById(R.id.red_img);
              ImageView blueStone = (ImageView) findViewById(R.id.blue_img);
              ImageView yellowStone = (ImageView) findViewById(R.id.yellow_img);
              ImageView skull = (ImageView) findViewById(R.id.skull_img);
              ImageView backgroundImage = (ImageView) findViewById(R.id.parchment_background);
              ImageView backgroundScoreImage = (ImageView) findViewById(R.id.score_parchment);
              ImageView spcl1 = (ImageView) findViewById(R.id.special_1);
              ImageView spcl2 = (ImageView) findViewById(R.id.special_2);
              ImageView oceanBackground = (ImageView) findViewById(R.id.ocean_image);

              staticImages = new ArrayList<>();
              staticImages.add(redStone);
              staticImages.add(blueStone);
              staticImages.add(yellowStone);
              staticImages.add(skull);
              staticImages.add(backgroundImage);
              staticImages.add(backgroundScoreImage);
              staticImages.add(spcl1);
              staticImages.add(spcl2);
              staticImages.add(oceanBackground);
        }

        private void setStaticImages(){
              Drawable img1 =  ResourcesCompat.getDrawable(getResources(), R.drawable.mouth, null);
              Drawable img2 =  ResourcesCompat.getDrawable(getResources(), R.drawable.nose, null);
              Drawable img3 =  ResourcesCompat.getDrawable(getResources(), R.drawable.eye, null);
              Drawable img4 =  ResourcesCompat.getDrawable(getResources(), R.drawable.display_skull, null);
              Drawable img5 =  ResourcesCompat.getDrawable(getResources(), R.drawable.parchment, null);
              Drawable img6 =  ResourcesCompat.getDrawable(getResources(), R.drawable.parchment, null);
              Drawable img7 =  ResourcesCompat.getDrawable(getResources(), R.drawable.penguin, null);
              Drawable img8 =  ResourcesCompat.getDrawable(getResources(), R.drawable.pirate, null);
              Drawable img9 =  ResourcesCompat.getDrawable(getResources(), R.drawable.island_ocean_1, null);
              Drawable img10 =  ResourcesCompat.getDrawable(getResources(), R.drawable.pirateship, null);

              List<Drawable> images = new ArrayList<>();
                   images.add(img1);
                   images.add(img2);
                   images.add(img3);
                   images.add(img4);
                   images.add(img5);
                   images.add(img6);
                   images.add(img7);
                   images.add(img8);
                   images.add(img9);


              for(int x = 0; x < staticImages.size(); x++){
                   ImageView img = staticImages.get(x);
                   img.setImageDrawable(images.get(x));
              }

             gameOverCards = new ArrayList<>();
             gameOverCards.add(img10);

         }

         private void assignCards(){
             List<ImageView> images = new ArrayList<>();
             List<TextView> texts = new ArrayList<>();

             ImageView img1 = (ImageView) findViewById(R.id.card1);
             ImageView img2 = (ImageView) findViewById(R.id.card2);
             ImageView img3 = (ImageView) findViewById(R.id.card3);
             ImageView img4 = (ImageView) findViewById(R.id.card4);
             ImageView img5 = (ImageView) findViewById(R.id.card5);
             ImageView img6 = (ImageView) findViewById(R.id.card6);
             ImageView img7 = (ImageView) findViewById(R.id.card7);
             ImageView img8 = (ImageView) findViewById(R.id.card8);
             ImageView img9 = (ImageView) findViewById(R.id.card9);
             ImageView img10 = (ImageView) findViewById(R.id.card10);
             ImageView img11 = (ImageView) findViewById(R.id.card11);
             ImageView img12 = (ImageView) findViewById(R.id.card12);


             images.add(img1);
             images.add(img2);
             images.add(img3);
             images.add(img4);
             images.add(img5);
             images.add(img6);
             images.add(img7);
             images.add(img8);
             images.add(img9);
             images.add(img10);
             images.add(img11);
             images.add(img12);

             TextView txt1 = (TextView) findViewById(R.id.card1_txt);
             TextView txt2 = (TextView) findViewById(R.id.card2_txt);
             TextView txt3 = (TextView) findViewById(R.id.card3_txt);
             TextView txt4 = (TextView) findViewById(R.id.card4_txt);
             TextView txt5 = (TextView) findViewById(R.id.card5_txt);
             TextView txt6 = (TextView) findViewById(R.id.card6_txt);
             TextView txt7 = (TextView) findViewById(R.id.card7_txt);
             TextView txt8 = (TextView) findViewById(R.id.card8_txt);
             TextView txt9 = (TextView) findViewById(R.id.card9_txt);
             TextView txt10 = (TextView) findViewById(R.id.card10_txt);
             TextView txt11 = (TextView) findViewById(R.id.card11_txt);
             TextView txt12 = (TextView) findViewById(R.id.card12_txt);


             texts.add(txt1);
             texts.add(txt2);
             texts.add(txt3);
             texts.add(txt4);
             texts.add(txt5);
             texts.add(txt6);
             texts.add(txt7);
             texts.add(txt8);
             texts.add(txt9);
             texts.add(txt10);
             texts.add(txt11);
             texts.add(txt12);

            gameCards = new ArrayList<>();

            for(int x = 0; x < images.size(); x++){
                ImageView img = images.get(x);
                 TextView txt = texts.get(x);


                 MatchImageCard card = new MatchImageCard(img, txt, x+1);
                gameCards.add(card);



            }


       }


}

As you can see i have 9 ImageViews that i need to access at the beginning to setup layout. I don't know how this may be doing too much work since i access them once then forget about them. As well, i have 12 TextViews and 12 ImageViews that i need to constantly handle. to save time i created a class MatchImageCard, where each object contains 1 textView and 1 ImageView that exist in the xml Layout. Just hearing myself talk allows me to realize that i am doing a lot of work, but i don't know how to reduce the workload on the main thread. if i have to run new Threads, WHERE. on the 9 ImageViews in the beginning, is it where i assign them or Set them? I can make do with any advice offered.

  • use asynchtask to assign then at setup – Divyesh Patel Dec 27 '16 at 07:16
  • have you tried using [traceview](https://developer.android.com/studio/profile/traceview.html)? – thepoosh Dec 27 '16 at 07:17
  • Follow @Divyesh suggestion, your code is in onCreate, so its working on main thread. – Mrinmoy Dec 27 '16 at 07:17
  • assign in pre, get drawables in doinback, and setimages in post – Bernardo Trevino Dec 27 '16 at 07:18
  • its irritating i get same results, no matter how many different ways i go about it – Bernardo Trevino Dec 27 '16 at 07:20
  • what i haven't done is an asyncTask for each of the 9 imageViews, i usually try all 9 in 1 asyncTask. should i try that. – Bernardo Trevino Dec 27 '16 at 07:22
  • what is your GAME class code? – Divyesh Patel Dec 27 '16 at 07:23
  • What is the resolution of all those images? As you are putting all the images in resource folder, internal memory issue is coming. Try to use url for setting images. It is not a good approach. And if it is really needed to put images statically, try to use images with small size. – Sneha Sarkar Dec 27 '16 at 07:25
  • it a simple class, you create it and the forget about it. at creation gets a certain number of objects say 12, and assigns images but enum value. then all you do outside class is call to find image of card. say findCard(int num). it sends u drawable location, forexample R.drawable.shark; – Bernardo Trevino Dec 27 '16 at 07:27
  • they are 108 pixels by 162 pixels – Bernardo Trevino Dec 27 '16 at 07:30
  • Go through [this answer](http://stackoverflow.com/a/21126690/5460053) and see whether it is really a concern. – Monish Kamble Dec 27 '16 at 07:38
  • It turns out that i was not performing AsyncTask correctly. i didn't know about the onProgressUpdate() so i was performing the code that should have been done there in either the doinbackground or postExecute. Either way, i no longer get the skip frames or too much work done on main thread messages. but i am still facing problems with my program. since these problems no longer deal with this question i consider this question closed and answered. Thank you everyone for your help. – Bernardo Trevino Dec 29 '16 at 07:16

2 Answers2

2

You are doing too much work in the main thread. you should use asynctask and to save time you should use Picasso or Glide.

specially for the code inside the loops. Ex:

          for(int x = 0; x < staticImages.size(); x++){
               ImageView img = staticImages.get(x);
               Picasso.with(context).load(images.get(x)).into(img);

          }
zMabrook
  • 932
  • 7
  • 9
2

As the error message suggest you are doing too much work in the Main thread (or UI thread). Move your code outside of the UI Thread using an AsyncTask. Other possible solution... you are using the emulator, I often had this problem with it, just check if you have the same issue with a real device.

Laurent
  • 1,661
  • 16
  • 29