java - 我的应用程序显示 "The application may be doing too much work on its main thread.",我不知道代码的哪一部分导致了此问题

标签 java android android-profiler

嗯,我是 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 操作系统主线程上运行。当您在主线程上进行大量处理时,会出现此警告。主要是因为从数据库加载数据。所以在你的情况

    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/Adapter。

关于java - 我的应用程序显示 "The application may be doing too much work on its main thread.",我不知道代码的哪一部分导致了此问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58703215/

相关文章:

java - 同步块(synchronized block)内的同步块(synchronized block)

Android 全屏对话框确认和不屑一顾的操作

android - kotlin-无法替换 RecyclerView 适配器中的 Fragment

android - Cordova "Local Notifications"插件?

android-studio - 如何在 Flutter 项目中使用 Android Studio 3 中的 Android Profiler 和 Logcat

android - Android Profiler工具可用于Android Studio中的flutter应用程序吗?

java - Android AlertManager 通过重启、更新等

java - 我怎样才能打印这些整个数组?

java - Java中可以有变量属性名吗?如果没有,为什么?