我的APP上有这个例外......我不知道为什么会发生这种情况。
java.lang.IllegalStateException: 适配器的内容已更改,但ListView未收到通知。确保不从后台线程修改适配器的内容,而只是从UI线程修改。
在ListView(2131492879)
,类android.widget.ListView
中使用适配器类com.agencialanave.AdaptadorListaApps
我尝试了很多选项,但我没有设法修复它。
类:Apps.java
public class Apps
{
public Bitmap Colorbanda;
public Bitmap Logo;
public Bitmap LogoBasura;
public String NombreAPP;
public float MemoriaUsada;
public String NombrePaquete;
public File Apk;
public Apps(Bitmap colorbanda, Bitmap logo,String nombreAPP, float memoriaUsada, Bitmap logoBasura, String nombrePaquete, File apk)
{
super();
Colorbanda = colorbanda;
Logo = logo;
LogoBasura = logoBasura;
NombreAPP = nombreAPP;
MemoriaUsada = memoriaUsada;
NombrePaquete = nombrePaquete;
Apk = apk;
}
public Apps(Bitmap colorbanda, Bitmap logo,String nombreAPP, float memoriaUsada, Bitmap logoBasura, String nombrePaquete)
{
super();
Colorbanda = colorbanda;
Logo = logo;
LogoBasura = logoBasura;
NombreAPP = nombreAPP;
MemoriaUsada = memoriaUsada;
NombrePaquete = nombrePaquete;
}
public Bitmap getColorbanda() {
return Colorbanda;
}
public void setColorbanda(Bitmap colorbanda) {
Colorbanda = colorbanda;
}
public Bitmap getLogo() {
return Logo;
}
public void setLogo(Bitmap logo) {
Logo = logo;
}
public Bitmap getLogoBasura() {
return LogoBasura;
}
public void setLogoBasura(Bitmap logoBasura) {
LogoBasura = logoBasura;
}
public String getNombreAPP() {
return NombreAPP;
}
public void setNombreAPP(String nombreAPP) {
NombreAPP = nombreAPP;
}
public float getMemoriaUsada() {
return MemoriaUsada;
}
public void setMemoriaUsada(float memoriaUsada) {
MemoriaUsada = memoriaUsada;
}
public String getNombrePaquete() {
return NombrePaquete;
}
public void setNombrePaquete(String nombrePaquete) {
NombrePaquete = nombrePaquete;
}
public File getApk() {
return Apk;
}
public void setApk(File apk) {
Apk = apk;
}
}
类:AdaptadorListaApps.java
public class AdaptadorListaApps extends ArrayAdapter<Apps>
{
private static Activity Context;
ArrayList<Apps> listaApps;
public AdaptadorListaApps(Activity context, ArrayList<Apps> lista)
{
super(context, R.layout.layout_list, lista);
Context = context;
listaApps = lista;
}
class ViewHolder
{
ImageView fotoBandaColor,fotoLogoApp,fotoDelete;
TextView nombreApp, memoriaUsada;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View item =convertView;
ViewHolder holder;
if (item == null)
{
LayoutInflater inflater = Context.getLayoutInflater();
item = inflater.inflate(R.layout.layout_list, null);
holder = new ViewHolder();
holder.fotoBandaColor = (ImageView)item.findViewById(R.id.bandaColor);
holder.fotoLogoApp = (ImageView)item.findViewById(R.id.logoApp);
holder.fotoDelete = (ImageView)item.findViewById(R.id.borrarApp);
holder.nombreApp = (TextView)item.findViewById(R.id.nombreApp);
holder.memoriaUsada = (TextView)item.findViewById(R.id.memoriaUsada);
item.setTag(holder);
}
else
{
holder=(ViewHolder) item.getTag();
}
holder.fotoBandaColor.setImageBitmap(listaApps.get(position).getColorbanda());
holder.fotoLogoApp.setImageBitmap(listaApps.get(position).getLogo());
holder.fotoDelete.setImageBitmap(listaApps.get(position).getLogoBasura());
holder.memoriaUsada.setText(TamañoAppEnRam(listaApps.get(position).getMemoriaUsada()) + " MB ( " + PorcentageMemoria(listaApps.get(position).getMemoriaUsada())+"% )");
if(listaApps.get(position).getMemoriaUsada() < 4)
{
holder.memoriaUsada.setTextColor(Color.parseColor("#56E000"));
}
else if(listaApps.get(position).getMemoriaUsada() > 7)
{
holder.memoriaUsada.setTextColor(Color.RED);
}
else
{
holder.memoriaUsada.setTextColor(Color.parseColor("#FF8000"));
}
holder.nombreApp.setText(listaApps.get(position).getNombreAPP());
return item;
}
}
更新列表视图的代码
private class GetRunningApps extends AsyncTask<String, ArrayList<Apps>, String>
{
@Override
protected String doInBackground(String... params) throws IllegalStateException
{
listaapps.clear();
listaapps = new ArrayList<Apps>();
cont = 0;
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningTasks = activityManager.getRunningAppProcesses();
PackageManager pm = context.getPackageManager();
Drawable icon;
Bitmap logo;
float memoria = 0;
String nombrePaquete;
MB = 0;
int pids[] = new int[1];
adaptadorLista = new AdaptadorListaApps(activity,listaapps);
for (RunningAppProcessInfo appInfo : runningTasks)
{
try
{
if(appInfo.importance == RunningAppProcessInfo.IMPORTANCE_SERVICE || appInfo.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND)
{
icon = pm.getApplicationIcon(appInfo.processName);
logo = ((BitmapDrawable) icon).getBitmap();
CharSequence c = pm.getApplicationLabel
(
pm.getApplicationInfo(appInfo.processName,PackageManager.GET_META_DATA)
);
pids[0] = appInfo.pid;
nombrePaquete = appInfo.processName;
android.os.Debug.MemoryInfo[] memoryInfoArray = activityManager.getProcessMemoryInfo(pids);
for(android.os.Debug.MemoryInfo pidMemoryInfo: memoryInfoArray)
{
memoria = (float)pidMemoryInfo.getTotalPss()/1024;
if(pidMemoryInfo.getTotalPss()/1024 <= 3)
{
listaapps.add(new Apps(bandaVerde, logo , c.toString() , memoria ,logoDelete, nombrePaquete, new File(getActivity().getPackageManager().getApplicationInfo(nombrePaquete, PackageManager.GET_META_DATA).publicSourceDir)));
}
else if((pidMemoryInfo.getTotalPss()/1024 >= 7))
{
listaapps.add(new Apps(bandaRoja, logo , c.toString() , memoria ,logoDelete,nombrePaquete, new File(getActivity().getPackageManager().getApplicationInfo(nombrePaquete, PackageManager.GET_META_DATA).publicSourceDir)));
}
else if((pidMemoryInfo.getTotalPss()/1024 > 3 && pidMemoryInfo.getTotalPss()/1024 < 7))
{
listaapps.add(new Apps(bandaNaranja, logo , c.toString() , memoria ,logoDelete,nombrePaquete, new File(getActivity().getPackageManager().getApplicationInfo(nombrePaquete, PackageManager.GET_META_DATA).publicSourceDir)));
}
}
cont++;
MB += memoria;
if(cont % 2 == 0)
{
try
{
publishProgress();
}
catch(IllegalStateException e){}
}
}
}
catch (Exception e) {}
}
cont = 0;
return null;
}
@Override
protected void onProgressUpdate(ArrayList<Apps>... values)
{
try
{
adaptadorLista = new AdaptadorListaApps(activity,listaapps);
listaAplicaciones.setAdapter(adaptadorLista);
adaptadorLista.notifyDataSetChanged();
}
catch(IllegalStateException e){}
}
@Override
protected void onPostExecute(String result)
{
adaptadorLista = new AdaptadorListaApps(activity,listaapps);
adaptadorLista.sort(new OrderList());
listaAplicaciones.setAdapter(adaptadorLista);
}
}
请帮助我......谢谢
答案 0 :(得分:1)
您永远不应在后台线程上更改视图或其任何基础数据源的属性。
请勿操纵listaaps
中的变量onBackground
。只能在onPostExecute
或onProgressUpdate
。
请勿操纵adaptorLista
中的变量onBackground
。只能在onPostExecute
或onProgressUpdate
。