gpt4 book ai didi

java - 应用程序在执行 AsyncTask 后不显示任何内容

转载 作者:行者123 更新时间:2023-11-30 00:09:03 25 4
gpt4 key购买 nike

这是一个简单的小部件,显示从外部存储中获取的一组位图。它基本上可以工作,但会监视 App 在其主线程上做太多工作,所以我把东西放在 AsyncTask 中,但是小部件仍然是空的,没有加载位图。这是有效的代码:

public class PhotosLoader /*extends AsyncTask<Void, Void ,ArrayList<Bitmap>> */ {
private Context mContext;
private RemoteViews views;
private ArrayList<Bitmap> images;

public PhotosLoader (Context context, RemoteViews views){
mContext = context;
this.views = views;
}

public ArrayList<Bitmap> LoadPhotos() {
PhotosReader PR = new PhotosReader();
images = PR.getPhotosArray(mContext);
PhotosModifier PM = new PhotosModifier();
ArrayList<Bitmap> bitmaps = PM.getResizedPhotosBitmap(images);
return bitmaps;
}

public void inflatePhotos(ArrayList<Bitmap> bitmaps){
views.setImageViewBitmap(R.id.imageView, bitmaps.get(0));
views.setImageViewBitmap(R.id.imageView2, bitmaps.get(1));
views.setImageViewBitmap(R.id.imageView3, bitmaps.get(2));
views.setImageViewBitmap(R.id.imageView4, bitmaps.get(3));
views.setImageViewBitmap(R.id.imageView5, bitmaps.get(4));
}
}

onUpdate 方法:

 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
PhotosLoader PL = new PhotosLoader(context, views);
ArrayList<Bitmap> bitmaps = PL.LoadPhotos();
PL.inflatePhotos(bitmaps);
}
}

还有一些代码没有:

public class PhotosLoader extends AsyncTask<Void, Void ,ArrayList<Bitmap>> {
private Context mContext;
private RemoteViews views;
private ArrayList<Bitmap> images;

public PhotosLoader (Context context, RemoteViews views){
mContext = context;
this.views = views;
}

@Override
protected ArrayList<Bitmap> doInBackground(Void... voids) {
PhotosReader PR = new PhotosReader();
images = PR.getPhotosArray(mContext);
PhotosModifier PM = new PhotosModifier();
ArrayList<Bitmap> bitmaps = PM.getResizedPhotosBitmap(images);
return bitmaps;
}

@Override
protected void onPostExecute(ArrayList<Bitmap> bitmaps) {
super.onPostExecute(bitmaps);
views.setImageViewBitmap(R.id.imageView, bitmaps.get(0));
views.setImageViewBitmap(R.id.imageView2, bitmaps.get(1));
views.setImageViewBitmap(R.id.imageView3, bitmaps.get(2));
views.setImageViewBitmap(R.id.imageView4, bitmaps.get(3));
views.setImageViewBitmap(R.id.imageView5, bitmaps.get(4));
}
}

更新时:

   @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
new PhotosLoader(context, views).execute();
Log.d("Main","onUpdate");
}
}

这些只是项目中的代码更改,我第一次实现 AsyncTask,所以我知道我可能做错了什么,看起来很简单

最佳答案

我认为问题在于您试图在 onUpdate 方法返回 RemoteView 对象后更新它。您应该返回已经构建并填写了所需数据的它。

也许尝试一种不同的方法,而不是传递位图并将 Uri 设置为 ImageView 上的图像?

编辑:实际上,另一个问题可能是您实际上并没有在任何地方更新小部件 :D 您创建了 RemoteView,然后将其删除。你在调用 appWidgetManager.updateAppWidget 吗?

关于java - 应用程序在执行 AsyncTask 后不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48490904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com