Mã của tôi là:Android: Volley: Tại sao volley imageLoader phải được gọi từ chuỗi chính?
class MyService extends Service{
public void onCreate(){
new ImageLoader(mRequestQueue, new VolleyLruCache(cacheSize))
.get(url, new ImageListener(){..});
}
}
tôi mong đợi hoạt động tốt, nhưng nó ném IllegalStateException ngoại lệ. Vì vậy, hãy mở nguồn đầy đủ về bóng chuyền, tìm mục này.
[ImageLoader.java]
public ImageContainer get(String requestUrl, ImageListener imageListener,
int maxWidth, int maxHeight) {
// only fulfill requests that were initiated from the main thread.
throwIfNotOnMainThread(); //<- why???
final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);
// Try to look up the request in the cache of remote images.
Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
if (cachedBitmap != null) {
// Return the cached bitmap.
ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
imageListener.onResponse(container, true);
return container;
}
// The bitmap did not exist in the cache, fetch it!
ImageContainer imageContainer =
new ImageContainer(null, requestUrl, cacheKey, imageListener);
// Update the caller to let them know that they should use the default bitmap.
imageListener.onResponse(imageContainer, true);
// Check to see if a request is already in-flight.
BatchedImageRequest request = mInFlightRequests.get(cacheKey);
if (request != null) {
// If it is, add this request to the list of listeners.
request.addContainer(imageContainer);
return imageContainer;
}
// The request is not already in flight. Send the new request to the network and
// track it.
Request<?> newRequest =
new ImageRequest(requestUrl, new Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
onGetImageSuccess(cacheKey, response);
}
}, maxWidth, maxHeight,
Config.RGB_565, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
onGetImageError(cacheKey, error);
}
});
mRequestQueue.add(newRequest);
mInFlightRequests.put(cacheKey,
new BatchedImageRequest(newRequest, imageContainer));
return imageContainer;
}
Tôi không hiểu throwIfNotOnMainThread() .. Tại sao imageLoader bóng phải được gọi từ các chủ đề chính?
Cảm ơn.
java.lang.IllegalStateException: ImageLoader phải được gọi từ chuỗi chính. tại com.android.volley.toolbox.ImageLoader.throwIfNotOnMainThread (ImageLoader.java:467) tại com.android.volley.toolbox.ImageLoader.get (ImageLoader.java:189) tại com.android.volley.toolbox. ImageLoader.get (ImageLoader.java:171) tại android.os.Handler.dispatchMessage (Handler.java:102) tại android.os.Looper.loop (Looper.java:136) tại android.os.HandlerThread. chạy (HandlerThread.java:61) – JuL