2

我有一个从数据库中检索到的图像 URL,我想在图像视图中显示它。我为此目的使用 Glide 库,但我有一段Exception时间doInBackground正在运行。

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));
                }
            }
        }
    }
}

我的日志:

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

4

1 回答 1

2

您不能调用.into(ImageView)后台线程。
我很确定您的日志中有这样的内容:

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
    ...

要修复它,将 PIMAGE 字符串存储在Card然后存储在onPostExecute

Card card = lcard.get(i);
Glide.with(Main.this).load(card.imageUrl).into(card.productThumbnailImage);
于 2015-07-06T12:10:36.700 回答