我的应用程序显示"The application may be doing too much work on its main thread.",我不知道代码的哪一部分导致了这种情况



好吧,我是Android编码的新手,我正在开发一个应用程序来教授如何种植一些植物/花朵。我使用SQLite数据库来存储数据,但我用游标调用类中的信息,除此之外,我不知道如何执行,所以我的应用程序真的很慢,我想这是游标的错误,但我无法修复。对不起,我的英语不好,我来自巴西。

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
}
}
}
} 

编辑:根据要求在下面记录日志。

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

问题从这一行开始:

final Cursor cursor = dab.alldata();

您直接从MainThread访问数据库。在android中,MainThread应该只用于布局更改和android框架填充。你总是需要访问数据库或Web服务器之类的东西,你需要在不同的线程中访问,而不是在MainThread。你可以在android中开始检查AssyncTasks,你会发现解决方案是在不同的线程中访问这些类型的数据。

我希望它能有所帮助!

I/Choreographer: Skipped 73 frames!  The application may be doing too much work on its main thread.

以上消息基本上是在Android的MainThread/UI线程工作过载时发出的。原因可能是复杂的非UI工作、繁重的动画等。

要检查是哪个部分导致的,您可以在运行应用程序时检查CPU活动。检查这个强大的官方链接

官方视频教程Android性能

投票,如果它有助于

谢谢。快乐编码。

相关内容

  • 没有找到相关文章