获取NULL指针异常

时间:2013-10-09 12:54:56

标签: android gridview nullpointerexception

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

我在这一行中得到Null Pointer Exception

 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">

6 个答案:

答案 0 :(得分:1)

您没有初始化LayoutInflater

 LayoutInflater mInflater = (context).getLayoutInflater();

答案 1 :(得分:1)

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

public MyGeneralFragmentImageAdapter(Context c){
    mycontext = c;
    mInflator = LayoutInflater.from(c);
}

答案 2 :(得分:1)

您忘记初始化LayoutInflator

   mInflater = LayoutInflator.from(context);

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

答案 3 :(得分:1)

试试这个,

LayoutInflater mInflater

public MyGeneralFragmentImageAdapter(Context c){
mycontext = c;
mInflater = LayoutInflater.from(c);
}

答案 4 :(得分:1)

因为mInflater具有null值,所以请尝试在构造函数

中传递Activity类
private LayoutInflater mInflater=null;

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

之后这是getview中的有效声明

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

答案 5 :(得分:0)

// try this way

convertView =LayoutInflater.from(mycontext).inflate(R.layout.row_grid,parent,false);