我有一个从数据库中检索到的图像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
答案 0 :(得分: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);