Error "The application may be Doing Too Much work on its main thread" Android

Asked

Viewed 1,749 times

0

Good afternoon guys, I am in need of a help to better understand how to solve this error that appears in the end:

I/Choreographer: Skipped 94 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 115 frames!  The application may be doing too much work on its main thread.
E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc

Right after the frame error appears the app is closed giving error. I wonder if anyone has gone through this before to help me.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_radio_no_ar, container, false);
        listViewPrograms = (ListView) rootView.findViewById(R.id.programacao_list);


        am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
        afChangeListener = new AudioManager.OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int focusChange) {
                if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
                    stopPlaying();
                } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                    stopPlaying();
                }
            }
        };

        if (player != null) {
            player.stop();
        }

        radioList = new Radio(getActivity()).getAll();

        if (radioList.size() > 0) {
            updateRadio();
            mountView();
        }

        return rootView;
    }

Play on the stream:

private void startPlaying() {
        if(urlStreaming == null) {
            urlStreaming = objRadio.getStreaming();
        }

        player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        threadRadio = Thread.currentThread();
        threadRadio = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    player.setDataSource(urlStreaming);
                } catch (IllegalArgumentException e) {
                    notifyErroPlayer();
                } catch (SecurityException e) {
                    notifyErroPlayer();
                } catch (IllegalStateException e) {
                    notifyErroPlayer();
                } catch (IOException e) {
                    notifyErroPlayer();
                }
                try {
                    player.prepare();
                    final int result = focusOnPlay();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED && player != null) {
                                player.start();
                                updateListPrograms();
                                shape = getDraw(getContext(), R.drawable.stop_button);
                                buttonPlay.setImageDrawable(shape);
                                setStateAudio(true);
                            }
                        }
                    });
                } catch (IllegalStateException e) {
                    notifyErroPlayer();
                } catch (IOException e) {
                    notifyErroPlayer();
                }
            }
        });
        threadRadio.start();
    }

Updatelistprograms that is where start and causes the main thread error

private void updateListPrograms() {
   final Handler handlerProgramacao = new Handler();
   handlerProgramacao.post(new Runnable() {
      @Override
      public void run() {
          inittask = new InitTask();
          inittask.execute();
      }
   });
}
  • This indicates that you are running time-consuming processing on main thread. Usually this is solved by moving these processes to another thread. This alone does not justify the app closing with error. Ask the question logcat to know what mistake is.

  • I have the Main Thread, it starts by checking if there is a radio programming list in the database, if it does not exist it searches in the json and saves. After that it starts that threadRadio = Thread.currentThread(); threadRadio = new Thread(new Runnable() { , and inside that is where the "lag" in the application, when I start the application initTask, this is too much for the Main Thread?

  • Edit the question and put that part of code, as well as the error.

  • Okay, I’ll do it!

  • ready, edited and put what you asked in order of execution!

  • You create a new thread but then use runOnUiThread.

  • Could you explain me better? this is the mistake? I don’t understand much of thread yet, I started working with android 1 month.

  • All that is inside runOnUiThread runs on the main thread, if this processing is delayed it will be the cause of this information. This does not cause "error", how much a box is opened to inform that the app does not respond.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.