2011-10-21 158 views
2

我嘗試以下方式,但我得到空指針異常在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; 

回答

0
static class viewHolder { 
     ImageView iv = null ; 
     TextView tv=null; 
} 


public View getView(int position, View convertView, ViewGroup parent) { 
     Bitmap bm=null; 
     viewHolder vh; 
     View v; 
        if(convertView==null){ 
         vh = new viewHolder(); 
         @SuppressWarnings("static-access") 
         LayoutInflater li = (LayoutInflater)mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE); 


         v = li.inflate(R.layout.category_icon, null); 
         vh.tv= (TextView)v.findViewById(R.id.icon_text); 

         vh.iv = (ImageView)v.findViewById(R.id.icon_image); 

         v.setTag(vh); 
        } 
        else 
        { 
         vh = (viewHolder)convertView.getTag(); 
         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(); 
         } 
        vh.iv.setImageBitmap(bm); 
        vh.tv.setText(CategorylogoActivity.namearray[0]); 

        return v; 

你必須使用viewHolder樣的對象,當convertView不爲空,你的IV ImageView的爲空。所以你對null對象使用setImageBitmap()。試試我的代碼。

HTH。

+0

感謝您對我的查詢做出快速而準確的回答。感謝您的分配。 –

0

你不應該在你的適配器做的URLConnection,這會盡量做到倍數連接,導致getView()會在您每次顯示適配器這一觀點時調用。在一個單獨的類中處理連接。

相關問題