Skip to content Skip to sidebar Skip to footer

Skipped 147 Frames! The Application May Be Doing Too Much Work On Its Main Thread

I understand the meaning of this error. I found many similar questions here at stackoverflow.com and I have tried to implement the answers those were suggested but still I am getti

Solution 1:

This type of error will come if your main thread doing so much work. Basically skip frames will happen maximum time when we test our app in emulator, because its already slow and unable to handle huge operation like a device.

I saw a simple problem in your code. We know onPostExecute() runs on MainThread, But still you use runOnUiThread(new Runnable() in onPostExecute() .

You doinbackground method returns an ArrayList , but your onpostexecute hold a string..

Change it as my onPostExecute' parameter that is the ArrayList(mylist)you use in your adapter.

EDIT

classLoadAllProductsextendsAsyncTask<String, String, ArrayList<HashMap<String, String>>> {

    ArrayList<HashMap<String, String>> mylist = newArrayList<HashMap<String, String>>();
    protected ArrayList<HashMap<String, String>> doInBackground(String... args){
    HttpClientclient=newDefaultHttpClient();
    HttpGetgetData=newHttpGet("your_url");
    HttpResponseresponse= client.execute(getData);
    BufferedReaderbr=newBufferedReader(newInputStreamReader(response.getEntity().getContent()));
    Stringdata="";

    while((data = br.readLine()) != null){
    JsonArrayarr=newJsonArray(data);
       for(int i=0;i<arr.length();i++)
            {                     
                HashMap<String, String> map = newHashMap<String, String>();    
                JSONObjecte= arr.getJSONObject(i);

                Stringfile_name= e.getString(TAG_FILE_NAME);
                Stringsender= e.getString(TAG_SENDER);
                Stringsubject= e.getString(TAG_SUBJECT);


                map.put(TAG_SENDER, sender);
                map.put(TAG_SUBJECT, subject);

                mylist.add(map);            

    }

    return mylist;

}

 protectedvoidonPostExecute(ArrayList<HashMap<String, String>> mylist) {
            String[] from = { TAG_SENDER, TAG_SUBJECT };
            int[] to = { android.R.id.text1, android.R.id.text2 };


            ListAdapteradapter=newSimpleAdapter(AllProductsActivity.this, mylist,
                    android.R.layout.simple_list_item_2, from , to);
                  setListAdapter(adapter);

              pDialog.dismiss();

        }

Solution 2:

I'm sorry this is not a super informed answer, but I have 2 suggestions. First, eliminate the runOnUiThread call, because onPostExecute already runs on the ui thread (that is the point of the method). That will not help with any frame skipping, but at least you can get rid of some unneeded code.

Second, create the ListAdapter in doInBackground. I doubt it will make much difference, but you might as well get as much off the UI thread as possible. So, instead of AsyncTask<String, String, ArrayList<HashMap<String, String>>> you will use AsyncTask<String, String, ListAdapter>. That leaves only 2 method calls in onPostExecute, pDialog.dismiss and setListAdapter. You can't get any more efficient than that. If it still skips frames, I would blame the debugger and move on.

Post a Comment for "Skipped 147 Frames! The Application May Be Doing Too Much Work On Its Main Thread"