javaandroidandroid-profiler

My app is showing "The application may be doing too much work on its main thread.", and I don't know what part of the code is causing this


Well, I'm new to Android coding and I'm developing an app to teach how to plant some plants/flower. I'm using the SQLite Database for storing the data, but I'm calling the information in class by cursors, I don't know how to do it other than this, so my app is really really slow, I guess it's cursors fault, but I can't fix this. Sorry for my bad english, I'm from Brazil :D.

package com.lmenegalli.homeplanting;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;

import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class FlowersSelectedActivity extends AppCompatActivity {

    ListView listViewSelected;

    int listCounter=0;
    String nomeFlor;
    String categoria;
    ArrayList<String> listItem;

    DataBase dab = new DataBase(this);


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

       final Cursor cursor = dab.alldata();

       //String categoriaFlor = cursor.getString(7);

        listViewSelected=(ListView)findViewById(R.id.array_full);

        final ArrayList<String> arrayListSelected = new ArrayList<>();



        Bundle b = getIntent().getExtras();

        if (b != null) {
                categoria = b.getString("categoria");





            final String aux = categoria;
        }

//        Log.d("Teste categoria", categoria);

        //arrayListSelected.add("teste");




    //=======================================================================
            //Teste do CRUD





        /*Flores flores = new Flores();
        flores.setId(3);
        dab.deleteFlor(flores);*/
        Toast.makeText(FlowersSelectedActivity.this, "Count: "+cursor.getCount(), Toast.LENGTH_SHORT).show();
        if(cursor.getCount()==0){
            Toast.makeText(getApplicationContext(),"NO DATA", Toast.LENGTH_LONG).show();
        } else{
            while(cursor.moveToNext()){
                if(cursor.getString(7).equals(categoria)) {
                    nomeFlor = cursor.getString(1);
                    arrayListSelected.add(nomeFlor);

                }
            }
        }

        ArrayAdapter arrayAdapterSelected = new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayListSelected);

        listViewSelected.setAdapter(arrayAdapterSelected);

        listViewSelected.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intentSelected = new Intent(FlowersSelectedActivity.this, ImageCard.class);

 String nomeList =(String) adapterView.getItemAtPosition(i);
//I did this because my data was sorted in database, but in ListView not, so when I clicked in something that was sorted, it always get the information of the first item of the list.
                //Log.d("Nome",nomeList);
                    switch (i) {
                        default:

                            cursor.moveToPosition(i);
                            while(!nomeList.equals(cursor.getString(1))) {
                                cursor.moveToNext();
// I guess the problem is here, but I don't know what is wrong.
                            }
                            if(nomeList.equals(cursor.getString(1))){
                                String nome = cursor.getString(1);
                                String descricao = cursor.getString(2);
                                String nomeCientifico = cursor.getString(3);
                                String imagem = cursor.getString(4);
                                String comoPlantar = cursor.getString(5);
                                String imagemPlantio = cursor.getString(6);


                                intentSelected.putExtra("nome", nome);
                                intentSelected.putExtra("descricao", descricao);
                                intentSelected.putExtra("nomeCientifico", nomeCientifico);
                                intentSelected.putExtra("imagem", imagem);
                                intentSelected.putExtra("comoPlantar", comoPlantar);
                                intentSelected.putExtra("imagemPlantio", imagemPlantio);
                                intentSelected.putExtra("categoria", categoria);
                                cursor.close();
                            }
                            break;
                    }
                startActivityForResult(intentSelected, 1);
                }

        });
       // Toast.makeText(FlowersSelectedActivity.this, "Salvo com Sucesso", Toast.LENGTH_LONG).show();


    //========================================================================
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle(categoria);
    }

    public void voltar(View view){
            Intent intentVoltar = new Intent(this, MainActivity.class);
        getApplicationContext().startActivity(intentVoltar);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        int id = item.getItemId();

        if(id == R.id.home){
            this.finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if(resultCode == ImageCard.RESULT_OK){
                if(data.getStringExtra("categoriaa") == categoria){
                    categoria = data.getStringExtra("categoria");
                }
            }
            if (resultCode == ImageCard.RESULT_CANCELED) {
                //Write your code if there's no result
            }
        }
    }

} 

EDIT: Logcat below as requested.

2019-11-04 23:50:33.867 11400-11400/com.lmenegalli.homeplanting I/zygote:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:106)
2019-11-04 23:50:33.867 11400-11400/com.lmenegalli.homeplanting I/zygote:     at void android.os.Looper.loop() (Looper.java:164)
2019-11-04 23:50:33.867 11400-11400/com.lmenegalli.homeplanting I/zygote:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6647)
2019-11-04 23:50:33.867 11400-11400/com.lmenegalli.homeplanting I/zygote:     at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2019-11-04 23:50:33.867 11400-11400/com.lmenegalli.homeplanting I/zygote:     at void com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run() (RuntimeInit.java:438)
2019-11-04 23:50:33.867 11400-11400/com.lmenegalli.homeplanting I/zygote:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:811)
2019-11-04 23:50:34.722 11400-11405/com.lmenegalli.homeplanting I/zygote: Do partial code cache collection, code=5KB, data=29KB
2019-11-04 23:50:34.724 11400-11405/com.lmenegalli.homeplanting I/zygote: After code cache collection, code=5KB, data=29KB
2019-11-04 23:50:34.724 11400-11405/com.lmenegalli.homeplanting I/zygote: Increasing code cache capacity to 128KB
2019-11-04 23:50:34.814 11400-11441/com.lmenegalli.homeplanting D/OpenGLRenderer: HWUI GL Pipeline
2019-11-04 23:50:35.032 11400-11441/com.lmenegalli.homeplanting I/Adreno: QUALCOMM build                   : bca571b, I47556f00c5
    Build Date                       : 04/30/18
    OpenGL ES Shader Compiler Version: EV031.22.00.01
    Local Branch                     : 
    Remote Branch                    : quic/LA.BR.1.3.7_rb1.11
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
2019-11-04 23:50:35.150 11400-11441/com.lmenegalli.homeplanting I/Adreno: PFP: 0x004ff087, ME: 0x004ff065
2019-11-04 23:50:35.164 11400-11441/com.lmenegalli.homeplanting I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2019-11-04 23:50:35.165 11400-11441/com.lmenegalli.homeplanting I/OpenGLRenderer: Initialized EGL, version 1.4
2019-11-04 23:50:35.165 11400-11441/com.lmenegalli.homeplanting D/OpenGLRenderer: Swap behavior 2
2019-11-04 23:50:35.409 11400-11441/com.lmenegalli.homeplanting D/vndksupport: Loading /vendor/lib/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
2019-11-04 23:50:40.714 11400-11405/com.lmenegalli.homeplanting I/zygote: Do partial code cache collection, code=33KB, data=54KB
2019-11-04 23:50:40.716 11400-11405/com.lmenegalli.homeplanting I/zygote: After code cache collection, code=33KB, data=54KB
2019-11-04 23:50:40.716 11400-11405/com.lmenegalli.homeplanting I/zygote: Increasing code cache capacity to 256KB
2019-11-04 23:50:40.903 11400-11400/com.lmenegalli.homeplanting D/Teste drawable: mipmap/babosa_circle
2019-11-04 23:50:40.903 11400-11400/com.lmenegalli.homeplanting D/Teste drawable 2: mipmap/comoplantarbabosa
2019-11-04 23:50:41.586 11400-11405/com.lmenegalli.homeplanting I/zygote: Do full code cache collection, code=123KB, data=98KB
2019-11-04 23:50:41.587 11400-11405/com.lmenegalli.homeplanting I/zygote: After code cache collection, code=112KB, data=62KB
2019-11-04 23:50:42.412 11400-11400/com.lmenegalli.homeplanting I/Choreographer: Skipped 73 frames!  The application may be doing too much work on its main thread.
2019-11-04 23:50:42.460 11400-11405/com.lmenegalli.homeplanting I/zygote: Do partial code cache collection, code=121KB, data=74KB
2019-11-04 23:50:42.466 11400-11405/com.lmenegalli.homeplanting I/zygote: After code cache collection, code=121KB, data=74KB
2019-11-04 23:50:42.466 11400-11405/com.lmenegalli.homeplanting I/zygote: Increasing code cache capacity to 512KB
2019-11-04 23:50:52.672 11400-11441/com.lmenegalli.homeplanting D/OpenGLRenderer: endAllActiveAnimators on 0x86c71480 (RippleDrawable) with handle 0x884de8b0

Solution

  • The android app works on Android OS main thread. this warning comes when you are doing to much processing on the main thread. Mostly it cause is loading data from database. So in your case

        final Cursor cursor = dab.alldata();
    
    if(cursor.getCount()==0){
                Toast.makeText(getApplicationContext(),"NO DATA", Toast.LENGTH_LONG).show();
            } else{
                while(cursor.moveToNext()){
                    if(cursor.getString(7).equals(categoria)) {
                        nomeFlor = cursor.getString(1);
                        arrayListSelected.add(nomeFlor);
    
                    }
                }
            }
    

    The above piece of code or anywhere else you are getting data from the database or storing in it, this should be done in AsyncTask. I hope it will solve your problem.

    AsyncTask will do the processing in the background and send the result in the main thread so you can do whatever you want with the data.

    P.S don't try to update UI/Adaptor from AsyncTask doInBackground method.