简体   繁体   中英

Glide library stopped in AsyncTask.doInBackground

I have a Image URL that I retrieved from database and I want to show it inside image view. I use the Glide library for this purpose but I have an Exception while doInBackground is running.

public class Main extends Fragment implements OnClickListener {

    private static String url = "http://timit.ir/select/select_pro";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "product";
    private static final String TAG_PID = "Pro_ID";
    public static final String TAG_PIMAGE = "Url";
    public static final String TAG_PNAME = "Pro_Name";
    public static final String TAG_PRICE = "Price";
    public static final String TAG_DESCRIPTION = "description";
    public static final String TAG_OLDPRICE = "oldPrice";
    public static final String TAG_COMPANYNAME = "companyName";
    public List<Card> cardList;

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.frag_main, container, false);

        l1 = (LinearLayout)rootView.findViewById(R.id.horizontal1);
        l2 = (LinearLayout)rootView.findViewById(R.id.horizontal2);

        new LoadAllProducts().execute();

        return rootView;
    }

    class LoadAllProducts extends AsyncTask<String, String, List<Card>> {
        @Override protected List<Card> doInBackground(String... args) {

            cardList = new ArrayList<Card>();
            JSONObject json = jParser.makeHttpRequest(url, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All Products: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    products = json.getJSONArray(TAG_PRODUCTS);

                    // looping through All Products
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        Card card = new Card(getActivity());

                        Log.d("url:", c.getString(TAG_PIMAGE));
                        Glide.with(Main.this).load(c.getString(TAG_PIMAGE)).into(card.productThumbnailImage);

                        card.productId = c.getString(TAG_PID);
                        card.productName.setText(c.getString(TAG_PNAME));
                        card.price.setText(c.getString(TAG_PRICE));
                        card.description.setText(c.getString(TAG_DESCRIPTION));
                        card.companyName.setText(c.getString(TAG_COMPANYNAME));
                        card.oldPrice.setText(c.getString(TAG_OLDPRICE));

                        cardList.add(card);
                    }
                    return cardList;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return cardList;
        }

        @Override protected void onPostExecute(List<Card> lcard) {
            super.onPostExecute(lcard);
            if (lcard.size() != 0) {
                for (int i = 0; i < lcard.size(); i++) {
                    l1.addView(lcard.get(i));
                }
            }
        }
    }
}

My log:

07-02 11:38:59.506: D/url:(4967): http://timit.ir/product_pic /iphone6plusthumnail.png

You cannot call .into(ImageView) on a background thread.
I'm pretty sure there's something like this in your log:

E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: net.twisterrob.app, PID: 11027
    java.lang.RuntimeException: An error occured while executing doInBackground()
    ...
Caused by:
    java.lang.IllegalArgumentException: You must call this method on the main thread
    ...

To fix it store the PIMAGE string in Card and then in onPostExecute :

Card card = lcard.get(i);
Glide.with(Main.this).load(card.imageUrl).into(card.productThumbnailImage);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM