0

这是我的 ImageaAdapter 类

public class MyGeneralFragmentImageAdapter extends BaseAdapter {

   private Context mycontext;
   private LayoutInflater mInflater;

public Integer[] ImageIds = {
        R.drawable.world, R.drawable.us, 
        R.drawable.european_flag

};

public String[] Imagename = {
        "World","USA","Europe"

};

public MyGeneralFragmentImageAdapter(Context c){
    mycontext = c;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return ImageIds.length;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder vh;

    if (convertView == null)
    {
    vh = new ViewHolder();
    convertView = mInflater.inflate(R.layout.row_grid,parent,false);
    vh.textview = (TextView)convertView.findViewById(R.id.grid_item_text);
    vh.imageView = (ImageView)convertView.findViewById(R.id.grid_item_image);
    convertView.setTag(vh); 
    }
    else 
    { 
    vh = (ViewHolder) convertView.getTag();  
    }       

    //ImageView imageView = new ImageView(mycontext);
    vh.imageView.setImageResource(ImageIds[position]);
    vh.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);


    vh.textview.setText(Imagename[position]);


    //imageView.setLayoutParams(new GridView.LayoutParams(100, 100));


    return convertView;

}

static class ViewHolder
{
       TextView textview;
       ImageView imageView;
}

 }

这是我的 row_grid.xml

  <LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:gravity="center_horizontal"
  >

 <Imageview 
   android:id="@+id/grid_item_image"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 </Imageview>

 <Textview
   android:id="@+id/grid_item_text"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:gravity="center_horizontal"
   android:text="TextView"
   android:textColor="#000000" />

我在这一行得到空指针异常

 convertView = mInflater.inflate(R.layout.row_grid,parent,false);

请帮助

更新

我已经更改了代码并初始化了布局 inflatter。代码工作正常,但它给了我二进制 XML 错误

<Imageview 
   android:id="@+id/grid_item_image"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

4

6 回答 6

1

用这个替换你的适配器构造函数:

public MyGeneralFragmentImageAdapter(Context c){
    mycontext = c;
    mInflator = LayoutInflater.from(c);
}
于 2013-10-09T13:00:05.030 回答
1

你忘了初始化你的LayoutInflator

   mInflater = LayoutInflator.from(context);

   convertView = mInflater.inflate(R.layout.row_grid,parent,false);
于 2013-10-09T13:00:50.530 回答
1

尝试这个,

LayoutInflater mInflater

public MyGeneralFragmentImageAdapter(Context c){
mycontext = c;
mInflater = LayoutInflater.from(c);
}
于 2013-10-09T13:00:54.267 回答
1

你没有初始化你的LayoutInflater

 LayoutInflater mInflater = (context).getLayoutInflater();
于 2013-10-09T12:57:05.573 回答
1

只是因为 mInflater 有空值,所以尝试在构造函数中传递 Activity 类

private LayoutInflater mInflater=null;

    public MyGeneralFragmentImageAdapter(Activity c){
        mInflater= c.getLayoutInflater();
    }

之后,这是 getview 中的有效声明

convertView = mInflater.inflate(R.layout.row_grid,parent,false);
于 2013-10-09T13:01:16.717 回答
0
// try this way

convertView =LayoutInflater.from(mycontext).inflate(R.layout.row_grid,parent,false);
于 2013-10-09T13:07:22.533 回答