Glide错误:java.lang.IllegalArgumentException-必须在后台线程上调用此方法
我正在使用glide将使用其资源ID的图像加载到位图中,我希望将位图传递给自定义视图类的成员函数 这是我的代码:Glide错误:java.lang.IllegalArgumentException-必须在后台线程上调用此方法,java,android,illegalargumentexception,android-glide,Java,Android,Illegalargumentexception,Android Glide,我正在使用glide将使用其资源ID的图像加载到位图中,我希望将位图传递给自定义视图类的成员函数 这是我的代码: try { Bitmap bm=Glide. with(getApplicationContext()). load(mThumbIds[position]). asBitmap(). into(width, height). // Wid
try {
Bitmap bm=Glide.
with(getApplicationContext()).
load(mThumbIds[position]).
asBitmap().
into(width, height). // Width and height
get();
drawView.setImg(bm);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
每当我尝试运行它时,都会出现以下错误:
无法启动活动组件信息{…}:
java.lang.IllegalArgumentException:必须在
背景线程
如何修复此问题?您不能在主线程上调用get()
,因为它执行长时间运行的任务,并且可能导致性能问题
相反,考虑子类,或者使用<代码>()/<代码>代替<代码> GET()/<代码>。code>into()将异步加载映像,您可以使用自定义子类在
onReasourceReady
中调用相应的成员函数
当调用onLoadFailed
或onLoadCleared
时,请确保清除位图(通常通过向成员函数传递null
)