这是我的 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">