2014-12-04 70 views
1

我有个问题关于GridView的错误的GridView Android上

public Class MyGribView extends BaseAdapter(){ 
... 
... 

public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView imgView; 
    imgView=(ImageView)convertView; 

    Bitmap bm = BitmapFactory.decodeFile(filepath[position]); 
    Log.d("filePath",filepath[position]+""); 

    imgView.setImageBitmap(bm); 

    return imgView; 
} 
} 

我上外部的Android到阵列字符串文件路径[](链接图像)确定所有的图像,但具有在线路imgView.setImageBitmap(bm)的问题。我该如何解决它?

+1

什么错误信息,你得到什么?在线imgView.set错误 – 2014-12-04 02:52:18

+0

。 。 。 java.lang.NullPointException – 2014-12-04 03:21:59

+0

我检查了filepath []。没关系。 – 2014-12-04 03:23:19

回答

1

使用自定义适配器:

grid_item

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/imgView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true"/> 

</LinearLayout> 

MyGribView

class MyGribView extends BaseAdapter{ 

    private Context context; 
    private String[] filepath; 

    public MyGribView(Context context,String[] filepath){ 
     this.context=context; 
     this.filepath=filepath; 
    } 

    @Override 
    public int getCount() { 
     return filepath.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return filepath[position]; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if(convertView==null){ 
      holder = new ViewHolder(); 
      convertView = LayoutInflater.from(context).inflate(R.layout.grid_item,null); 
      holder.imgView = (ImageView) convertView.findViewById(R.id.imgView); 
      convertView.setTag(holder); 
     }else{ 
      holder =(ViewHolder) convertView.getTag(); 
     } 
     holder.imgView.setImageBitmap(decodeFile(filepath[position])); 
     return convertView; 
    } 
    class ViewHolder{ 
     ImageView imgView; 
    } 
    public Bitmap decodeFile(String path) { 
     try { 
      // Decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(path, o); 
      // The new size we want to scale to 
      final int REQUIRED_SIZE = 70; 
      // Find the correct scale value. It should be the power of 2. 
      int scale = 1; 
      while (o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >=REQUIRED_SIZE) 
       scale *= 2; 
      // Decode with inSampleSize 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = scale; 
      return BitmapFactory.decodeFile(path, o2); 
     } catch (Throwable e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 
+0

错误“内存不足......字节分配”。我该如何解决它?谢谢 – 2014-12-04 13:46:48

+0

检查:http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object – 2014-12-04 13:52:36

+0

谢谢。我修好了它 。 – 2014-12-04 15:16:09

0

你从来没有真正夸大视图或实例化一个视图。性能和内存使用情况也很糟糕,为列表视图中的每个项目创建一个位图,回收您的位图。

下面的代码可能会解决您的问题,但它也是低效的。如果我是你,我会查看视图持有者模式。与ViewHolder设计模式

public Class MyGribView extends BaseAdapter(){ 
    ... 
    ... 

    public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView imgView = new ImageView(context); 
     imgView=(ImageView)convertView; 

     Bitmap bm = BitmapFactory.decodeFile(filepath[position]); 
     Log.d("filePath",filepath[position]+""); 

     imgView.setImageBitmap(bm); 

     return imgView; 
    } 
} 
+0

这似乎不起作用。 :)。你能告诉我如何让GribView外部的图像变得简单吗?我是初学者。我搜索了Google,但还不清楚。对不起,我英文不好 – 2014-12-04 03:45:02

+0

你应该检查convertView是否是ImageView的一个实例,而且它不是null。 convertView将经常为空,这就是为什么你总是得到一个空指针。如果convertView不符合这些要求,则只应创建一个新视图。 – 2014-12-04 03:52:58

+0

@NathanWalters即使这是一个糟糕的例子,我的答案只是让他工作的最快方法。这就是为什么我建议他看看一个视图模式,以减少创建的视图和优化gridview – SemaphoreMetaphor 2014-12-04 05:52:18