这个java android代码怎么了?

时间:2013-09-26 17:33:45

标签: android

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View gridView;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(
                      Context.LAYOUT_INFLATER_SERVICE);
        gridView = new View (mContext);
        gridView = inflater.iflate(R.layout.gridview_item,null);
    }else {
        gridView = convertView;
    }
    TextView textView = (TextView)
    gridView.findViewById(R.id.grid_item_text);
    textView.setText(mThumbIds_Strings[position]);
    ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
    imageView.setImageResource(mThumbIds[position]);
    return gridView;
}

我们想要膨胀。我们将使用已存在的布局gridview_item,并且是Gridview的对象。当我尝试运行此代码时,会出现以下错误:

LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(
    Context.LAYOUT_INFLATER_SERVICE);

TextView textView = (TextView)

ImageView imageView = (ImageView)

1 个答案:

答案 0 :(得分:0)

嘿,你没有给出错误日志,但我认为问题在于inflate方法,如下所示进行更改。除此之外,不需要gridView = new View (mContext);

   @Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View gridView;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        gridView = inflater.inflate(R.layout.gridview_item, parent, false);
    }else {
        gridView = convertView;
    }
    TextView textView = (TextView)
    gridView.findViewById(R.id.grid_item_text);
    textView.setText(mThumbIds_Strings[position]);
    ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
    imageView.setImageResource(mThumbIds[position]);
    return gridView;
}