我嘗試以下方式,但我得到空指針異常在iv.setImageBitmap(bm)。 我嘗試使用網格視圖和佈局Inflater.I也嘗試設置圖像與文本我也做了調試過程,但仍然無法獲得解決方案。請檢查下面的代碼。如何在gridview中設置圖像的下方有文字?
public class CategoryAdapter extends BaseAdapter {
private Context mContext;
public static final int ACTIVITY_CREATE = 10;
int id=-1;
public CategoryAdapter(Context c) {
mContext = c;
}
public int getCount() {
return CategorylogoActivity.logoarray.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
Bitmap bm=null;
ImageView iv = null ;
TextView tv=null;
View v;
if(convertView==null){
@SuppressWarnings("static-access")
LayoutInflater li = (LayoutInflater)mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.category_icon, null);
tv= (TextView)v.findViewById(R.id.icon_text);
iv = (ImageView)v.findViewById(R.id.icon_image);
}
else
{
v = convertView;
}
try {
URL aURL = new URL(CategorylogoActivity.logoarray[position]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
// Buffered is always good for a performance plus.
BufferedInputStream bis = new BufferedInputStream(is);
// Decode url-data to a bitmap.
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
iv.setImageBitmap(bm);
tv.setText(CategorylogoActivity.namearray[0]);
return v;
感謝您對我的查詢做出快速而準確的回答。感謝您的分配。 –