我的应用程序显示“该应用程序可能在其主线程上做过多的工作。”,我不知道代码的哪一部分导致了此问题。

2019年5月19日 7点热度 0条评论

好吧,我是Android编码的新手,而且我正在开发一个应用程序,以教授如何种植一些植物/花卉。我正在使用SQLite数据库存储数据,但我是用游标调用类中的信息,除此之外我不知道该怎么做,所以我的应用程序真的很慢,我想这是游标的错,但我无法解决。
对不起,我的英语不好,我来自巴西: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
            }
        }
    }

} 

编辑:根据要求下面的Logcat。

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

解决方案如下:

android应用程序可在Android OS主线程上运行。当您在主线程上执行大量处理时,会出现此警告。主要原因是从数据库加载数据。所以你的情况

    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);

                }
            }
        }

上面的代码或从数据库中获取数据或将其存储在其他任何地方的代码,都应在
AsyncTask中完成。我希望它能解决您的问题。

AsyncTask将在后台进行处理并将结果发送到主线程中,因此您可以对数据进行任何处理。

P.S不要尝试从
AsyncTask doInBackground方法更新UI /适配器。