重写了BaseAdapter @Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView;
if(arg1 == null){
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(45,45));
imageView.setAdjustViewBounds(false);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}else{
imageView = (ImageView) arg1;
}
Resources res=getResources();  
int i=res.getIdentifier(arr[arg0],"drawable",getPackageName());  
imageView.setImageResource(R.drawable.default_pic);
return imageView;
}
以上正确的
现在想用布局文件来完成,我把它修改如下 public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View v ;
if(arg1 == null){
v = (View) findViewById(R.layout.items_img);
ImageView iv1 = (ImageView) v.findViewById(R.id.ItemImage1);
ImageView iv2 = (ImageView) v.findViewById(R.id.ItemImage2);
iv1.setImageResource(R.drawable.default_pic);
iv2.setImageResource(R.drawable.checkbox1);
}else{
v = (ImageView) arg1;
}
return v;
}items_img.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
    android:id="@+id/ItemImage1"
    android:layout_width="100dip"
    android:layout_height="100dip"
    android:layout_centerHorizontal="true"/>
<ImageView
    android:id="@+id/ItemImage2"
    android:layout_width="40dip"
    android:layout_height="40dip"
    android:src="@drawable/checkbox1"
    />
</RelativeLayout>运行报错,说我这行空指针,不可能啊!
ImageView iv1 = (ImageView) v.findViewById(R.id.ItemImage1);

解决方案 »

  1.   

     v = (View) findViewById(R.layout.items_img);这里错了,v不是find出来的,是LayoutInflator加载出来的
    v = LayoutInflater.from(mContext).inflate(R.layout.home_grid_item, null);
      

  2.   

    要先手动 加载xml文件,然后再find,随便找一个demo 就可以看到了
      

  3.   

    要先手动 加载xml文件,然后再find,随便找一个demo 就可以看到了
      

  4.   

    1楼正解,或者直接获取系统服务
    LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(
    R.layout.gridview_pop, null, true);
    menuGrid = (GridView) menuView.findViewById(R.id.gridview);just like this